Identifying lists that have 3 elements in common in a lists of lists
<p>Hi</p>

<p>I have a list of lists. If there are subslists that have the first three elements in common , merge them into one list and add all the fourth elements.</p>

<p>The problem is best explained in code and the required output.</p>

<pre><code>a_list = [['apple', 50, 60, 7],
          ['orange', 70, 50, 8],
          ['apple', 50, 60, 12]]

# output:
# [['apple', 50, 60, 19], ['orange', 70, 50, 8]]
</code></pre>

<p>I already have code for a similiar problem (given to me by another user in stackoverflow some time ago), but i don't understand it completely so i'm unable to modify it accordingly. What this code does is it checks if the 0th and 2nd elements are the same, if they are, it merges the sublists, adding the 1st and 3th element.</p>

<pre><code>import defaultdict
data = [['42x120x1800', 50, '50x90x800', 60],
        ['42x120x1800', 8, '50x90x800', 10],
        ['2x10x800', 5, '5x9x80', 6]]

d = defaultdict(lambda :[0, 0])
for sub_list in data:
    key = (sub_list[0], sub_list[2])
    d[key][0] += sub_list[1]
    d[key][1] += sub_list[3]

new_data = [[key[0], val[0], key[1], val[1]] for key, val in d.iteritems()]
# [['2x10x800', 5, '5x9x80', 6], ['42x120x1800', 58, '50x90x800', 70]]
</code></pre>

<p>How should the code be modified to fit to my new problem? I'd really appreciate if you could also take the time and explain the code thoroughly, too. Thank you very much for your help!</p>
