简体   繁体   English

过滤python中的元组列表

[英]filtering a list of tuples in python

Am looking for a clean pythonic way of doing the following 我正在寻找一种干净的pythonic方法来执行以下操作

I have a list of tuples say : 我有一个元组列表说:

[(1,'a'), (1,'b'), (1,'c'), (2, 'd'), (5, 'e'), (5, 'f')]

I want to make a new list which discards tuples whose first key has been seen before. 我想创建一个新列表,该列表将丢弃之前已经看到过第一个键的元组。 So the o/p for the above would be: 因此,上述的o / p为:

[(1,'c'), (2,'d'), (5, 'f')]

Thanks! 谢谢!

A simple way would be creating a dictionary, since it will only keep the last element with the same key: 一种简单的方法是创建一个字典,因为它将只保留最后一个具有相同键的元素:

In [1]: l = [(1,'a'), (1,'b'), (1,'c'), (2, 'd'), (5, 'e'), (5, 'f')]

In [2]: dict(l).items()
Out[2]: [(1, 'c'), (2, 'd'), (5, 'f')]

Update: As @Tadeck mentions in his comment, since the order of dictionary items is not guaranteed, you probably want to use an ordered dictionary : 更新:正如@Tadeck在其评论中提到的那样,由于不能保证字典项目的顺序,因此您可能要使用有序字典

from collections import OrderedDict
newl = OrderedDict(l).items()

If you actually want to keep the first tuple with the same key (and not the last, your question is ambiguous), then you could reverse the list first, add it do the dictionary and reverse the output of .items() again. 如果您实际上想使用相同的键来保留第一个元组(而不是最后一个,则您的问题是模棱两可的),则可以首先反转列表,将其添加到字典中,然后再次反转.items()的输出。
Though in that case there are probably better ways to accomplish this. 尽管在这种情况下,可能有更好的方法可以完成此操作。

Using unique_everseen from itertools docs 使用unique_everseenitertools 文档

from itertools import ifilterfalse
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 ifilterfalse(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

a = [(1,'a'), (1,'b'), (1,'c'), (2, 'd'), (5, 'e'), (5, 'f')]

print list(unique_everseen(a,key=lambda x: x[0]))

Yielding 屈服

[(1, 'a'), (2, 'd'), (5, 'e')]

a nifty trick for one liner fetishists that keeps the order in place (I admit it's not very readable but you know...) 一位班轮专科医生的巧妙技巧,可以使顺序保持正确(我承认它不是很可读,但是您知道...)

>>> s = [(1,'a'), (1,'b'), (1,'c'), (2, 'd'), (5, 'e'), (5, 'f')]
>>> seen = set()
>>> [seen.add(x[0]) or x for x in s if x[0] not in seen]
[(1, 'a'), (2, 'd'), (5, 'e')]

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

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