Making a sequence of tuples unique by a specific element

无人久伴 提交于 2020-03-08 08:55:49

问题


So I have a tuple of tuples

a = ((1, 2), (7, 2), (5, 2), (3, 4), (8, 4))

I would like to remove all the tuples from 'a' which have the a common second element, except for one (any one of them).

For the example above I would like the new output a = ((1,2),(3,4))

In other words I would like to eliminate tuples which are considered duplicate elements in the second position of the tuple.

I would like to know the most efficient way to achieve this, and also like to know if I can do the same with lists instead of tuples?


回答1:


You could create a dictionary from your elements, with whatever you wanted to be unique as the key, then extracting the values. This works for anything where the 'unique' sub-element is hashable. Integers are hashable:

def unique_by_key(elements, key=None):
    if key is None:
        # no key: the whole element must be unique
        key = lambda e: e
    return {key(el): el for el in elements}.values()

This function is pretty generic; it can be used to extract 'unique' elements by any trait, as long as whatever the key callable returns can be used as a key in a dictionary. Order will not be preserved, and currently the last element per key wins.

With the above function you can use a operator.itemgetter() object or a lambda to extract your second value from each element. This then works for both a sequence of tuples and a sequence of lists:

from operator import itemgetter

unique_by_second_element = unique_by_key(a, key=itemgetter(1))

Demo:

>>> from operator import itemgetter
>>> a = ((1, 2), (7, 2), (5, 2), (3, 4), (8, 4))
>>> unique_by_key(a, key=itemgetter(1))
[(5, 2), (8, 4)]
>>> b = [[1, 2], [7, 2], [5, 2], [3, 4], [8, 4]]
>>> unique_by_key(b, key=itemgetter(1))
[[5, 2], [8, 4]]

Note that the function always returns a list; you can always convert that back by calling tuple() on the result.



来源:https://stackoverflow.com/questions/31499259/making-a-sequence-of-tuples-unique-by-a-specific-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!