鱼C论坛

 找回密码
 立即注册
查看: 1225|回复: 3

如何根据列表中字典的key对列表进行筛选

[复制链接]
发表于 2019-3-10 03:09:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
给定下面列表,不适用模块的情况下,如何对p列表排序(依照'shares'或者'prices'进行排序)

  1. p=[{'name':'ibm','shares':100,'prices':91.11},
  2.    {'name':'apple','shares':50,'prices':543.31},
  3.    {'name':'t','shares':150,'prices':21.51},
  4.    {'name':'mi','shares':30,'prices':441.16},
  5.    {'name':'360','shares':10,'prices':61.81},
  6.    {'name':'car','shares':80,'prices':221.51}
  7. ]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-10 03:50:20 | 显示全部楼层
本帖最后由 yjsx86 于 2019-3-10 04:00 编辑
  1. # 按照prices排序
  2. p = [{'name': 'ibm', 'shares': 100, 'prices': 91.11},
  3.      {'name': 'apple', 'shares': 50, 'prices': 543.31},
  4.      {'name': 't', 'shares': 150, 'prices': 21.51},
  5.      {'name': 'mi', 'shares': 30, 'prices': 441.16},
  6.      {'name': '360', 'shares': 10, 'prices': 61.81},
  7.      {'name': 'car', 'shares': 80, 'prices': 221.51}]

  8. new_p = sorted(p, key=lambda x: x['prices'])

  9. print(new_p)

  10. #结果
  11. [{'name': 't', 'shares': 150, 'prices': 21.51},
  12. {'name': '360', 'shares': 10, 'prices': 61.81},
  13. {'name': 'ibm', 'shares': 100, 'prices': 91.11},
  14. {'name': 'car', 'shares': 80, 'prices': 221.51},
  15. {'name': 'mi', 'shares': 30, 'prices': 441.16},
  16. {'name': 'apple', 'shares': 50, 'prices': 543.31}]

  17. #先按prices排序再按shares排序,把除了21.51和543.31的其他prices全部改为99.11
  18. p = [{'name': 'ibm', 'shares': 100, 'prices': 91.11},
  19.      {'name': 'apple', 'shares': 50, 'prices': 543.31},
  20.      {'name': 't', 'shares': 150, 'prices': 21.51},
  21.      {'name': 'mi', 'shares': 30, 'prices': 91.11},
  22.      {'name': '360', 'shares': 10, 'prices': 91.11},
  23.      {'name': 'car', 'shares': 80, 'prices': 91.11}]

  24. new_p = sorted(p, key=lambda x: (x['prices'], x['shares']))

  25. print(new_p)

  26. #结果
  27. [{'name': 't', 'shares': 150, 'prices': 21.51},
  28. {'name': '360', 'shares': 10, 'prices': 91.11},
  29. {'name': 'mi', 'shares': 30, 'prices': 91.11},
  30. {'name': 'car', 'shares': 80, 'prices': 91.11},
  31. {'name': 'ibm', 'shares': 100, 'prices': 91.11},
  32. {'name': 'apple', 'shares': 50, 'prices': 543.31}]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-3-10 03:54:22 | 显示全部楼层

  1. p_sort = []
  2. for i,each in enumerate(p):
  3.     p_sort.append([each['shares'],i])
  4. p_sort.sort()
  5. p2 = p[:]
  6. for i,each in enumerate(p_sort):
  7.     p[i]=p2[each[1]]
  8. print(p)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-10 09:54:56 | 显示全部楼层
http://www.runoob.com/python3/python3-att-list-sort.html

上面链接,网页最下面的 3 篇笔记,能不能帮助到你~

评分

参与人数 1荣誉 +2 鱼币 +3 贡献 +1 收起 理由
Stubborn + 2 + 3 + 1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 12:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表