简体   繁体   English

如何从python中的列表中删除重复项

[英]How to remove duplicates from list in python

I have a simple code here:我这里有一个简单的代码:

a=[['bcn09','113','shift1'],['bcn09','113','shift1'],['bps01','132','shift2']]

b=[]
for i in range (len(a)):
   if a[i] not in b:
      b.append([a[i]])


print (b)

The output i get is我得到的输出是

b=[['bcn09','113','shift1'],['bcn09','113','shift1'],['bps01','132','shift2']]

ie the same as a The output i need is即与a相同我需要的输出是

b=[['bcn09','113','shift1'],['bps01','132','shift2']]

What am i doing wrong?我究竟做错了什么?

Thanks in advance提前致谢

Very close!很接近! You could try this approach, hopefully it makes sense:您可以尝试这种方法,希望它有意义:

a=[['bcn09','113','shift1'],['bcn09','113','shift1'],['bps01','132','shift2']]

noDups = []
for i in a:
  if i not in noDups:
    noDups.append(i)
print(noDups)

Output:输出:

>>>[['bcn09', '113', 'shift1'], ['bps01', '132', 'shift2']]

There is no need to create a new list when appending to 'b'.附加到“b”时无需创建新列表。

Modify the below line from -修改以下行 -

b.append([a[i]])

to

b.append(a[i])

The new output is (which you want) -新的输出是(你想要的) -

[['bcn09', '113', 'shift1'], ['bps01', '132', 'shift2']]

This explains more about the 'in' membership test operator. 解释了有关“in”成员资格测试运算符的更多信息。

For the most complete answer to getting only unique values from an iterable, itertools gives a recipe (which can also be found in the more-itertools package on PyPI ) for this:对于仅从迭代中获取唯一值的最完整答案, itertools提供了一个配方(也可以在 PyPI 上的more-itertools包中找到):

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

This implementation is tested, optimised, maintains order, is a generator so it will work on infinite iterables or cases where you need to be lazy, and allows you to use a key function.此实现经过测试、优化、维护顺序,是一个生成器,因此它可以在无限迭代或需要懒惰的情况下工作,并允许您使用关键功能。

>>> a=[['bcn09','113','shift1'],['bcn09','113','shift1'],['bps01','132','shift2']]
>>> list(unique_everseen(tuple(item) for item in a))
[('bcn09', '113', 'shift1'), ('bps01', '132', 'shift2')]

Note the change to tuples so the elements are hashable and can be added to the set.请注意元组的更改,因此元素是可散列的并且可以添加到集合中。 You can of course reverse this at the end if needed, although in most cases I can imagine tuples will probably be fine.如果需要,您当然可以在最后反转这一点,尽管在大多数情况下,我可以想象元组可能会很好。 (In the same way, I am creating a list from the generator to show the output, but most of the time you should be able to just work with the generator directly). (同样,我正在从生成器创建一个列表来显示输出,但大多数时候您应该能够直接使用生成器)。

you can achive by using itertools你可以通过使用itertools来实现

import itertools
a = [['bcn09','113','shift1'],['bcn09','113','shift1'],['bps01','132','shift2']]

a.sort()
new_num = list(a for a,_ in itertools.groupby(a))
print("New List", new_num)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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