Python Forum
Adding List Element if Second part of the List Elements are the Same - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding List Element if Second part of the List Elements are the Same (/thread-31153.html)



Adding List Element if Second part of the List Elements are the Same - quest_ - Nov-25-2020

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..


RE: Adding List Element if Second part of the List Elements are the Same - bowlofred - Nov-25-2020

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)]]



RE: Adding List Element if Second part of the List Elements are the Same - quest_ - Nov-25-2020

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]



RE: Adding List Element if Second part of the List Elements are the Same - bowlofred - Nov-25-2020

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.