Abh*_*tia 3 python string tuples list
我有一个格式列表(浮点数,字符串).如何从列表中删除具有相同浮点值的重复项?
列表按浮点顺序排序.我想保留订单.
[(0.10507038451969995,
'Deadly stampede in Shanghai - Emergency personnel help victims.'),
(0.078586381821416265,
'Deadly stampede in Shanghai - Police and medical staff help injured people after the stampede.'),
(0.072031446647399661, '- Emergency personnel help victims.'),
(0.072031446647399661, 'Emergency personnel help victims.')]
Run Code Online (Sandbox Code Playgroud)
看看最后两个.
您可以使用,itertools.groupby因为您已经对这些值进行了排序.这是数据:
>>> lot
[(0.10507038451969995, 'Deadly stampede in Shanghai - Emergency personnel help victims.'),
(0.07858638182141627, 'Deadly stampede in Shanghai - Police and medical staff help injured people after the stampede.'),
(0.07203144664739966, '- Emergency personnel help victims.'),
(0.07203144664739966, 'Emergency personnel help victims.')]
Run Code Online (Sandbox Code Playgroud)
示范:
>>> import itertools
>>> [next(t) for _, t in itertools.groupby(lot, lambda x: x[0])]
[(0.10507038451969995,
'Deadly stampede in Shanghai - Emergency personnel help victims.'),
(0.07858638182141627,
'Deadly stampede in Shanghai - Police and medical staff help injured people after the stampede.'),
(0.07203144664739966, '- Emergency personnel help victims.')]
Run Code Online (Sandbox Code Playgroud)
这将为您提供组合在一起的第一个值.