如何从列表中相似项目的集合中保留唯一项目? [英] How to keep unique items from a collection of similar items in a list?
问题描述
我有一个类似以下的列表
I have a list like the following
a = [5,5,5,5,3,3,2,2,2,2,5,5,5,2,2,2,2,2]
我希望有以下输出列表
b = [5,3,2,5,2]
请注意,我尝试使用
list(OrderedDict.fromkeys(a))
这给了我
[5, 3, 2]
请注意,由于当时2nd 5或2nd 2并不唯一,因此请注意.
Notice that it does not consider the 2nd 5 or the 2nd 2 as they are non-unique by then.
我需要一种使机器理解的方法,当它遇到一个唯一元素(例如第一个"5")时,应将其存储在列表b中.因此,当遇到第一个"3"或第一个"2"时,它也应该将它们存储在列表b中.到此为止没有问题.当它遇到第二组"5"时,应将其5作为新元素存储在列表b中.第二组2也是如此.
I need a way to make the machine understand that as and when it encounters a unique element (say the first '5'), it should store it in list b. Consequently, when it encounters the first '3' or first '2', it should store them in list b as well. No problems until this point. As it encounters the second set of '5's, it should store that 5 as a new element in list b. Same goes for the second set of 2's.
有没有Python的方法可以做到这一点?
Is there a Pythonic way to do this?
推荐答案
除了进行for循环外,您还可以使用itertools groupby:
Other than just doing a for loop you could use itertools groupby:
import itertools
a = [5,5,5,5,3,3,2,2,2,2,5,5,5,2,2,2,2,2]
b = [x[0] for x in itertools.groupby(a)] # [5, 3, 2, 5, 2]
有关此文档的信息,请参见: https://docs .python.org/3/library/itertools.html#itertools.groupby
Documentation for this can be found here: https://docs.python.org/3/library/itertools.html#itertools.groupby
进行一些澄清.能够在列表中重复出现的次数的原因在文档(强调我的)中的这一段中得到了解释:
Some clarification. The reason this is able to count re-occurrences in the list is explained by this paragraph in the documentation (emphasis mine):
groupby()的操作类似于Unix中的uniq过滤器.它 每次键值生成一个中断或新组 功能更改 [...]
The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes [...]
这篇关于如何从列表中相似项目的集合中保留唯一项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!