python 字典的值是列表_python实现求和python如何通过列表中字典的值对列表进行排序...

5ce8cff0d02a0207.jpg

一. 按字典值排序(默认为升序)

x = {1:2, 3:4, 4:3, 2:1, 0:0}

1. sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) print sorted_x

输出结果:[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

如果要降序排序,可以指定reverse=True

电脑技术002pc网从python实现求和python如何通过列表中字典的值对列表进行排序分析来看,对python实现求和python如何通过列表中字典的值对列表进行排序的结果。

2. sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True) print sorted_x

输出结果:[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]

二. 或者直接使用list的reverse方法将sorted_x顺序反转

sorted_x.reverse()

三. 更为常用的方法是,用lambda表达式

sorted_x = sorted(x.iteritems(), key=lambda x : x[1]) print sorted_x

输出结果:[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

orted_x = sorted(x.iteritems(), key=lambda x : x[1], reverse=True) print sorted_x

输出结果:[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]

以上就是python如何通过列表中字典的值对列表进行排序的详细内容,更多请关注其它相关文章!

更多:python实现求和python如何通过列表中字典的值对列表进行排序

https://www.002pc.comhttps://www.002pc.com/python/1449.html

你可能感兴趣的列表,python,字典,排序,如何,通过

No alive nodes found in your cluster

0踩

0 赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值