我有一个元组列表,不幸的是包含重复项,如下所示:
[(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
问题是元组的第一个元素(基于0的排序)是我想要检查重复项的条目。所以,我可以看到:
(67, u'top-coldestcitiesinamerica')
(61, u'top-coldestcitiesinamerica')
..是重复的,我想删除其中一个(类似于set
)。所以,最后,我想要一个没有重复的元组的清单列表(即元组的第一个元素没有重复):
[(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c') (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion')]
我怎样才能以pythonic方式实现这一目标? 谢谢!
答案 0 :(得分:5)
您可以使用How do you remove duplicates from a list in whilst preserving order?中的set
方法,使用x[1]
作为唯一标识符:
def unique_second_element(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x[1] in seen or seen_add(x[1]))]
请注意,如果您想保留 last 次出现,也会显示OrderedDict
方法。第一次出现时,您必须反转输入,然后再次反转输出。
您可以通过支持key
函数来使其更加通用:
def unique_preserve_order(seq, key=None):
if key is None:
key = lambda elem: elem
seen = set()
seen_add = seen.add
augmented = ((key(x), x) for x in seq)
return [x for k, x in augmented if not (k in seen or seen_add(k))]
然后使用
import operator
unique_preserve_order(yourlist, key=operator.itemgetter(1))
演示:
>>> def unique_preserve_order(seq, key=None):
... if key is None:
... key = lambda elem: elem
... seen = set()
... seen_add = seen.add
... augmented = ((key(x), x) for x in seq)
... return [x for k, x in augmented if not (k in seen or seen_add(k))]
...
>>> from pprint import pprint
>>> import operator
>>> yourlist = [(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
>>> pprint(unique_preserve_order(yourlist, operator.itemgetter(1)))
[(67, u'top-coldestcitiesinamerica'),
(66, u'ecofriendlyideastocelebrateindependenceday-phpapp'),
(65, u'a-b-c-ca-d-ab-ea-d-c-c'),
(63,
u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'),
(62, u'ghgemissions'),
(58, u'infographicthe-stateofdigitaltransformationaltimetergroup'),
(57, u'culture'),
(55, u'cas-k-ihaveanidea'),
(54, u'trendsfor'),
(53, u'batteryimpedance'),
(52, u'evs-howey-full'),
(51, u'bericht'),
(49, u'classiccarinsurance'),
(47, u'uploaded_file'),
(46, u'x_file'),
(45, u's-s-main'),
(44, u'vehicle-propulsion')]
答案 1 :(得分:1)
作为替代答案,您可以使用itertools.groupby()
,如果您有一个庞大的列表,但这不如set
那么有用:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [next(g) for _,g in groupby(sorted(l,key=itemgetter(1)),itemgetter(1))]
[(65, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (53, u'batteryimpedance'), (51, u'bericht'), (55, u'cas-k-ihaveanidea'), (49, u'classiccarinsurance'), (57, u'culture'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (52, u'evs-howey-full'), (62, u'ghgemissions'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (45, u's-s-main'), (67, u'top-coldestcitiesinamerica'), (54, u'trendsfor'), (47, u'uploaded_file'), (44, u'vehicle-propulsion'), (46, u'x_file')]
答案 2 :(得分:0)
代码:
input_list = [(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
check_list = set()
result = []
for i in input_list:
if not i[1] in check_list:
result.append(i)
check_list.add(i[1])
import pprint
pprint.pprint(result)
输出:
$ python task4.py
[(67, u'top-coldestcitiesinamerica'),
(66, u'ecofriendlyideastocelebrateindependenceday-phpapp'),
(65, u'a-b-c-ca-d-ab-ea-d-c-c'),
(63,
u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'),
(62, u'ghgemissions'),
(58, u'infographicthe-stateofdigitaltransformationaltimetergroup'),
(57, u'culture'),
(55, u'cas-k-ihaveanidea'),
(54, u'trendsfor'),
(53, u'batteryimpedance'),
(52, u'evs-howey-full'),
(51, u'bericht'),
(49, u'classiccarinsurance'),
(47, u'uploaded_file'),
(46, u'x_file'),
(45, u's-s-main'),
(44, u'vehicle-propulsion')]
答案 3 :(得分:0)
我做得非常简单明了。
lst=[(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
lst2 = [] #empty list to fill with unique tuples
lst_banned = [] #empty list to fill with banned elements
for tup in lst:
if tup[-1] not in lst_banned:
lst_banned.append(tup[-1])
lst2.append(tup)
lst=lst2
del lst2
del lst_banned