Python Forum
Adding List Element if Second part of the List Elements are the Same
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding List Element if Second part of the List Elements are the Same
#1
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..
Reply
#2
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)]]
quest_ likes this post
Reply
#3
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]
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,514 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020