python中列表嵌套字典/列表排序,字典排序,列表排序。

本文详细讲解了如何使用Python的内置函数和lambda表达式对包含字典和嵌套列表的列表进行排序,包括按 tier_index 排序、uid排序,以及利用operator模块的itemgetter实现复杂键值排序。实例涵盖了多层嵌套的数据结构处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

列表排序 

列表嵌套字典/列表排序 使用 lambda 

# 列表内多层嵌套,字典和列表
list_num = [
    {'name': '中国', 'uid': 10003, 'extinfo': {'tier_index': [0, 4]}},
    {'name': '美国', 'uid': 10001, 'extinfo': {'tier_index': [1, 5]}},
    {'name': '日本', 'uid': 10005, 'extinfo': {'tier_index': [0, 5]}},
    {'name': '英国', 'uid': 10004, 'extinfo': {'tier_index': [3, 0]}},
    {'name': '德国', 'uid': 10002, 'extinfo': {'tier_index': [4, 5]}}
]
tier_index = sorted(list_num, key=lambda r: r['extinfo']['tier_index'], reverse=False)  # 升序 不写默认reverse=False
print(tier_index)
tier_index = sorted(list_num, key=lambda r: r['extinfo']['tier_index'], reverse=True)  # 降序 不写默认reverse=False
print(tier_index)
uid = sorted(list_num, key=lambda r: r['uid'], reverse=True)  # 降序 不写默认reverse=False
print(uid)
uid = sorted(list_num, key=lambda r: r['uid'])  # 升序 不写默认reverse=False
print(uid)

 运行结果

 

list_date = [2, 0, 1, 4, 5, 3]
print(f"原始数据:{list_date}")
list_date.sort()  # 升序/reverse=False
print(f"升序后数据:{list_date}")
list2 = sorted(list_date)  # 升序/reverse=False
print(f"sorted升序后数据:{list2}")
list_date.sort(reverse=True)  # 降序
print(f"降序后数据:{list_date}")
list2 = sorted(list_date, reverse=True)  # 降序
print(f"sorted,降序后数据:{list2}")

# 结果
原始数据:[2, 0, 1, 4, 5, 3]
升序后数据:[0, 1, 2, 3, 4, 5]
sorted升序后数据:[0, 1, 2, 3, 4, 5]
降序后数据:[5, 4, 3, 2, 1, 0]
sorted,降序后数据:[5, 4, 3, 2, 1, 0]

使用 operator 模块  安装   pip install operator  

from operator import itemgetter

# 列表内多层嵌套,字典和列表
list_num = [
    {'name': '中国', 'uid': 10003, 'extinfo': {'tier_index': [0, 4]}},
    {'name': '美国', 'uid': 10001, 'extinfo': {'tier_index': [1, 5]}},
    {'name': '日本', 'uid': 10005, 'extinfo': {'tier_index': [0, 5]}},
    {'name': '英国', 'uid': 10004, 'extinfo': {'tier_index': [3, 0]}},
    {'name': '德国', 'uid': 10002, 'extinfo': {'tier_index': [4, 5]}}
]
rows_by_name = sorted(list_num, key=itemgetter('uid'))
print(rows_by_name)
rows_by_name = sorted(list_num, key=itemgetter('uid'), reverse=True)
print(rows_by_name)

字典排序,嵌套列表

list_num1 = {'10003': [1, 0, 3, '中国'], '10001': [0, 3, 2, '美国'], '10004': [2, 4, 1, '英国'], '10000': [3, 1, 4, '日本']}
rows_by_uid = sorted(list_num1.items(), key=lambda x: x[1][2], reverse=True)  # 降序 不写默认reverse=False
print(rows_by_uid)

# 结果
[('10000', [3, 1, 4, '日本']), ('10003', [1, 0, 3, '中国']), ('10001', [0, 3, 2, '美国']), ('10004', [2, 4, 1, '英国'])]


# x: x[1][0] 和 x: x[1] 相同  排序列表内第一个值
[('10000', [3, 1, 4, '日本']), ('10004', [2, 4, 1, '英国']), ('10003', [1, 0, 3, '中国']), ('10001', [0, 3, 2, '美国'])]
# x: x[0] 排序字典key的值
[('10004', [2, 4, 1, '英国']), ('10003', [1, 0, 3, '中国']), ('10001', [0, 3, 2, '美国']), ('10000', [3, 1, 4, '日本'])]
# x: x[1][1] 排序列表内第二个值
[('10004', [2, 4, 1, '英国']), ('10001', [0, 3, 2, '美国']), ('10000', [3, 1, 4, '日本']), ('10003', [1, 0, 3, '中国'])]
# x: x[1][1] 排序列表内第三个值
[('10000', [3, 1, 4, '日本']), ('10003', [1, 0, 3, '中国']), ('10001', [0, 3, 2, '美国']), ('10004', [2, 4, 1, '英国'])]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值