繁体   English   中英

在列表中查找非唯一元素不起作用

[英]finding non-unique elements in list not working

我想在列表中找到非唯一元素,但我无法弄清楚为什么下面的代码部分没有发生这种情况。

>>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]
>>> for i in d:
...     if d.count(i) == 1:
...             d.remove(i)
... 
>>> d
[1, 2, 1, 2, 4, 4, 'a', 'b', 'a', 'b', 6, 3]

6 和 3 应该已被删除。 在哪里,如果我使用

d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c']

我得到了正确的答案。 请解释发生了什么,我很困惑!

我正在使用 python 2.7.5。

在迭代列表时删除列表的元素从来都不是一个好主意。 执行此操作的适当方法是使用带有列表理解collections.Counter

>>> from collections import Counter
>>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6, 'f', 3]
>>> # Use items() instead of iteritems() in Python 3
>>> [k for (k,v) in Counter(d).iteritems() if v > 1]
['a', 1, 2, 'b', 4]

如果您想按照它们在列表中出现的顺序保留重复元素:

>>> keep = {k for (k,v) in Counter(d).iteritems() if v > 1}
>>> [x for x in d if x in keep]
[1, 2, 1, 2, 4, 4, 'a', 'b', 'a', 'b']

我会试着解释为什么你的方法不起作用。 要理解为什么某些元素没有按原样删除,请想象我们要在循环遍历列表[a, b, b, c]时从列表中删除所有b 它看起来像这样:

+-----------------------+
|  a  |  b  |  b  |  c  |
+-----------------------+
   ^ (first iteration)

+-----------------------+
|  a  |  b  |  b  |  c  |
+-----------------------+
         ^ (next iteration: we found a 'b' -- remove it)

+-----------------------+
|  a  |     |  b  |  c  |
+-----------------------+
         ^ (removed b)

+-----------------+
|  a  |  b  |  c  |
+-----------------+
         ^ (shift subsequent elements down to fill vacancy)

+-----------------+
|  a  |  b  |  c  |
+-----------------+
               ^ (next iteration)

请注意,我们跳过了第二个b 一旦我们删除了第一个b ,元素就会向下移动,我们的for循环因此无法触及列表中的每个元素。 同样的事情发生在你的代码中。

更好地使用collections.Counter()

>>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]
>>> from collections import Counter
>>> [k for k, v in Counter(d).iteritems() if v > 1]
['a', 1, 2, 'b', 4]

另请参阅相关线程:

我只是想如果有人感兴趣,我会用集合理解添加我的方法。

>>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]
>>> d = list({x for x in d if d.count(x) > 1})
>>> print d
['a', 1, 2, 'b', 4]

我相信 Python 2.7 及更高版本的集合理解功能。

感谢所有的回答和评论!

想了一会儿,得到了另一个答案,以我以前编写代码的方式。 所以,我发布它。

d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]
e = d[:] # just a bit of trick/spice
>>> for i in d:
...     if d.count(i) == 1:
...             e.remove(i)
... 
>>> e
[1, 2, 1, 2, 4, 4, 'a', 'b', 'a', 'b']

@arshajii,你的解释让我想到了这个技巧。 谢谢!

你也可以这样做:

data=[1,2,3,4,1,2,3,1,2,1,5,6]
    first_list=[]
    second_list=[]
    for i in data:
        if data.count(i)==1:
            first_list.append(i)
        else:
            second_list.append(i)
            print (second_list)

结果

[1, 2, 3, 1, 2, 3, 1, 2, 1]

对于

>>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]

使用转换为集合会产生唯一项:

>>> d_unique = list(set(d))

可以使用列表理解找到非唯一项

>>> [item for item in d_unique if d.count(item) >1]
[1, 2, 4, 'a', 'b']

在 python3 中,使用dict.items()代替dict.iteritems()

iteritems()在 python3 中被删除,所以你不能再使用这个方法了。

    >>> d = [1, 2, 1, 2, 4, 4, 5, 'a', 'b', 'a', 'b', 'c', 6,'f',3]
    >>> from collections import Counter
    >>> [k for k, v in Counter(d).items() if v > 1]
    ['a', 1, 2, 'b', 4]

暂无
暂无

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

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