基于项目的python唯一列表 [英] python unique list based on item

查看:110
本文介绍了基于项目的python唯一列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单

old_list = [
        (1, 'AAA', None, 1),
        (2, 'AAA', 'x', 0),
        (5, 'AAB', 'z', 1),
        (6, 'ABB', 'x', 1),
        (9, 'ABB', 'x', 1)]

我要如何获得一个具有唯一i [1]和更大id i [0]的新列表,就像这样的结果

How I want get a new list have unique i[1] and the bigger id i[0], like this result

new_list = [
        (2, 'AAA', 'x', 0),
        (5, 'AAB', 'z', 1),
        (9, 'ABB', 'x', 1)]
]

有人可以帮助我吗?

推荐答案

您可以使用 输出

[(2, 'AAA', 'x', 0), (5, 'AAB', 'z', 1), (9, 'ABB', 'x', 1)]

如果old_list尚未排序,则可以这样排序

if the old_list is not sorted already, you can sort it like this

old_list = sorted([
        (1, 'AAA', None, 1),
        (2, 'AAA', 'x', 0),
        (5, 'AAB', 'z', 1),
        (6, 'ABB', 'x', 1),
        (9, 'ABB', 'x', 1)], key=itemgetter(1))

这篇关于基于项目的python唯一列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆