Python Forum

Full Version: Adding List Element if Second part of the List Elements are the Same
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have this list

newlist = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)]]]]

Here I want to compare second part of the list (this part: (1.57, 1.57, 0), (1.57, 1.57, 0), (1.57, 1.57, 1.57).. ) and if the second parts of the list are same? I want to add them together. For instance the list which I wrote above we have 2 times (0, 1.57, 0) so the new list should be like that:
newlist2 = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1],[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]]], (0, 1.57, 0), (0, 1.57, 0)# here instead of adding(0, 1.57, 0) again we can just put 2 for remembering we had 2 times (0, 1.57, 0)]
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)]]]

How can I do that in an efficient way
Thanks for helps..
Trying to do this entirely within a list seems possible, but annoying to me. You're treating the bit on the end like a dictionary key. So I'd recommend creating a real dictionary instead. Then if you need the lists later, reassemble them from the dictionary.

Actually storing the data in a dictionary seems much better than forcing everything into a four-deep nested list structure.

from collections import defaultdict
import pprint

newlist = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)],
[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)]]

info = defaultdict(list)

#dict from the list
for row in newlist:
    sublist, key = row
    info[key].append(sublist)

# reassmble the list
newlist = []
for key, value in info.items():
    sublist = value
    sublist.append(key)
    newlist.append(value)

pprint.pprint(newlist)
Output:
[[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (1.57, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], [[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)]]
It worked thanks Smile and is it possible to save in the list how many times we found second part. For intance:

Output:
[[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (1.57, 1.57, 0)], 1, [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], [[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], 2, [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)],1 [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)],1]
The way it is now, the "key" is at the end of all the added lists. So you could just count the size of the list and subtract 1.