如果每个元组中的第二项是重复的,如何从元组列表中删除元素?

时间:2013-02-26 13:13:44

标签: python list sorting duplicates tuples

如果每个元组中的第二项是重复的,如何从元组列表中删除元素?

例如,我有一个按第一个元素排序的列表,如下所示:

alist = [(0.7897897,'this is a foo bar sentence'),
(0.653234, 'this is a foo bar sentence'),
(0.353234, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar'),
(0.323234, 'this is a foo bar sentence'),]

所需的输出使元组具有最高的第1项,应为:

alist = [(0.7897897,'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar')]

1 个答案:

答案 0 :(得分:8)

如果您的alist已按第一个元素从最高到最低排序:

alist = [(0.7897897,'this is a foo bar sentence'),
(0.653234, 'this is a foo bar sentence'),
(0.353234, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar'),
(0.323234, 'this is a foo bar sentence'),]

seen = set()
out = []
for a,b in alist:
    if b not in seen:
        out.append((a,b))
        seen.add(b)

out现在是:

[(0.7897897, 'this is a foo bar sentence'),
 (0.325345, 'this is not really a foo bar')]