如何从一个元组列表中删除第二个元素重复的元组?

3

如何从一个元组列表中删除第二个元素是重复的元素?

例如,我有一个按照第一个元素排序的列表,看起来像这样:

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'),]

期望的输出是保留第一个元素最大值的元组,应为:
alist = [(0.7897897,'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar')]
1个回答

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')]

1
@Junuxx 我猜测这个列表是偶然排序的,但不能保证每次都会排序。 - heltonbiker
@Junuxx - 你说得对。最有趣的部分已经由OP完成了。然后就只是一个无聊的循环。 - eumiro
1
@heltonbiker - OP已经有了“我有一个按第一个元素排序的列表”- 我一开始没有意识到。 - eumiro
(好的,实际上原帖已经说明了列表已经排序好了...) - heltonbiker
我已经在做那个了,但我认为可能有更简洁的方法来完成这个任务,而不需要创建新的集合或列表 =) - alvas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接