简体   繁体   中英

Remove duplicates from list of lists of tuples

I've got a very long list that looks like this:

triangles = [[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)], 
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)], 
             [(4, 48, 53), (4, 48, 52), (4, 48, 52)], 
             [(4, 48, 52), (4, 48, 53), (4, 48, 53)], 
             [(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)], 
             [(4, 48, 53), (4, 48, 52), (4, 48, 52)], 
             [(4, 48, 52), (4, 48, 53), (4, 48, 53)], 
             [(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)]]

It contains lists of 3 coordinates for the corners of triangles. Now I need to remove all of the duplicate triangles. Just doing list(set(triangles)) does not work. It throws this error:

Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 96, in <module>
TypeError: unhashable type: 'list'

How do I remove duplicate triangles?

Lists are unhashable, so make everything a tuple:

list({tuple(sorted(x)) for x in triangles})

The sorting is there in case the vertices are in different orders. If different order vertices shouldn't be removed (or don't exist), then you can get rid of the sorting and replace the whole map with map(tuple, triangles) .

If you want the individual elements to be lists again, then use a list comprehension to make it that way:

[list(x) for x in {tuple(sorted(x)) for x in triangles}]

If all you're looking for are the specific tuples that are totally unique in the whole list, then you could use itertools.chain to "flatten" the list of lists into a single iterator to give to set :

>>> from itertools import chain
>>> list(set(chain(*triangles)))
[(-4, 48, 52), (4, 48, 52), (-4, 48, 53), (4, 48, 53)]

aplet's answer is the way to go but I'll add a rudimentary way. Since you were trying to use {} I'll assume that a duplicate triangle is 2 sub lists that are identical.

you could then jsut build a new 2d list with:

newList = []
for i in triangles:
    if i not in newList:
        newList.append(i)

If I understand correctly, you need to make list of tuples to be unique between in the whole list of lists. Then this is my working solution:

def remove_duplicates(list):       
    return [list(t) for t in (set(tuple(i) for i in lst))]

triangles = [[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)], 
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)], 
             [(4, 48, 53), (4, 48, 52), (4, 48, 52)], 
             [(4, 48, 52), (4, 48, 53), (4, 48, 53)], 
             [(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)], 
             [(4, 48, 53), (4, 48, 52), (4, 48, 52)], 
             [(4, 48, 52), (4, 48, 53), (4, 48, 53)], 
             [(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
             [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)]] 
             
print(remove_duplicates(triangles))

and the output is:

[[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)], 
 [(4, 48, 52), (4, 48, 53), (4, 48, 53)], 
 [(4, 48, 53), (4, 48, 52), (4, 48, 52)], 
 [(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)]]

So duplicated lists of tuples were removed from the the whole list of lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM