Python - 删除以相反顺序排列的列表重复项

时间:2022-11-11 04:41:32

Any ideas on tackling an issue I am facing in Python...

关于解决我在Python中遇到的问题的任何想法......

I have a nested list with items that are duplicated but in the opposite order (order stays the same but they switch in the middle):

我有一个嵌套列表,其中的项目是重复的但顺序相反(顺序保持不变,但它们在中间切换):

links = [['a', 'b', 'c', 'd'], ['c', 'd', 'a', 'b'], ['e', 'f', 'g', 'h'], ['g', 'h', 'e', 'f']]

Does anyone have a suggestion on the best way to remove the duplicates so I end up with:

有没有人有关于删除重复项的最佳方法的建议,所以我最终得到:

links = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']]

I want to detect the duplicate and remove it, the first instance needs to be kept exactly the same and in the same order, so in the above example I am removing list items 1 (['c', 'd', 'a', 'b']) and 3(['g', 'h', 'e', 'f'])

我想检测副本并将其删除,第一个实例需要保持完全相同且顺序相同,所以在上面的例子中我删除了列表项1(['c','d','a' ,'b'])和3(['g','h','e','f'])

Thanks in advance!

提前致谢!

3 个解决方案

#1


0  

Similar to the other response but maintains the order of the original lists.

与其他响应类似但保持原始列表的顺序。

def get_uniques(links):    
    my_set = set([])
    my_selection = []
    for l in links:
        s = "".join(sorted(l))
        if not s in my_set:
            my_set.add(s)
            my_selection.append(l)
    return my_selection

And a cryptic one-liner-like (probably more efficient):

和一个神秘的单线程(可能更有效):

scheck = set()
survivers = [ (l, scheck.add("".join(sorted(l))) ) 
                         for l in links if  not "".join(sorted(l)) in scheck ]
result = [a for a, _ in survivers]

#2


0  

You can first sort and convert to strings all the items in your list:

您可以先对列表中的所有项目进行排序并转换为字符串:

l = [ "".join(sorted(l)) for l in links ]

The conversion allows you to use 'set' that guarantee no duplicates:

转换允许您使用'set'来保证不重复:

l = list(set(l))

Finally, convert the strings back to list of characters :

最后,将字符串转换回字符列表:

l = [ list(l) for l in l ]

#3


0  

First sort the elements of list and then remove duplicate. Find my code and listsfinal represents your answer.

首先对列表元素进行排序,然后删除重复项。找到我的代码,listfinal代表你的答案。

links = [['a', 'b', 'c', 'd'], ['c', 'd', 'a', 'b'], ['e', 'f', 'g', 'h'], 

['g', 'h', 'e', 'f']]
links1=[]
for elements in links:
    elements=sorted(elements)
    links1.append(elements)
# print links1
linksfinal=[list(t) for t in set(tuple(element) for element in links1)]
print linksfinal

#1


0  

Similar to the other response but maintains the order of the original lists.

与其他响应类似但保持原始列表的顺序。

def get_uniques(links):    
    my_set = set([])
    my_selection = []
    for l in links:
        s = "".join(sorted(l))
        if not s in my_set:
            my_set.add(s)
            my_selection.append(l)
    return my_selection

And a cryptic one-liner-like (probably more efficient):

和一个神秘的单线程(可能更有效):

scheck = set()
survivers = [ (l, scheck.add("".join(sorted(l))) ) 
                         for l in links if  not "".join(sorted(l)) in scheck ]
result = [a for a, _ in survivers]

#2


0  

You can first sort and convert to strings all the items in your list:

您可以先对列表中的所有项目进行排序并转换为字符串:

l = [ "".join(sorted(l)) for l in links ]

The conversion allows you to use 'set' that guarantee no duplicates:

转换允许您使用'set'来保证不重复:

l = list(set(l))

Finally, convert the strings back to list of characters :

最后,将字符串转换回字符列表:

l = [ list(l) for l in l ]

#3


0  

First sort the elements of list and then remove duplicate. Find my code and listsfinal represents your answer.

首先对列表元素进行排序,然后删除重复项。找到我的代码,listfinal代表你的答案。

links = [['a', 'b', 'c', 'd'], ['c', 'd', 'a', 'b'], ['e', 'f', 'g', 'h'], 

['g', 'h', 'e', 'f']]
links1=[]
for elements in links:
    elements=sorted(elements)
    links1.append(elements)
# print links1
linksfinal=[list(t) for t in set(tuple(element) for element in links1)]
print linksfinal