如果每个元组中的第二项是重复的,如何从元组列表中删除元素?
例如,我有一个按第一个元素排序的列表,如下所示:
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')]
答案 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')]