在Python中的列表中对嵌套字典进行排序?(Sort nested dictionary inside a list in Python? [duplicate])

编程入门 行业动态 更新时间:2024-10-21 06:27:32
在Python中的列表中对嵌套字典进行排序?(Sort nested dictionary inside a list in Python? [duplicate])

这个问题在这里已有答案:

如何按Python中字典的值对字典列表进行排序? 17个答案 [ { "PricingOptions": { "Price": 51540.72, "Agents": [ 4056270 ] } }, { "PricingOptions": { "Price": 227243.14, "Agents": [ 4056270],} } ]

我怎样才能按价格排序..?

This question already has an answer here:

How do I sort a list of dictionaries by a value of the dictionary? 16 answers [ { "PricingOptions": { "Price": 51540.72, "Agents": [ 4056270 ] } }, { "PricingOptions": { "Price": 227243.14, "Agents": [ 4056270],} } ]

How can I sort according to Price..?

最满意答案

data = [ { "PricingOptions": { "Price": 51540.72, "Agents": [ 4056270 ] } }, { "PricingOptions": { "Price": 227243.14, "Agents": [ 4056270],} } ] newlist = sorted(data, key=lambda k: k['PricingOptions']["Price"]) print(newlist)

输出:

[{'PricingOptions': {'Price': 51540.72, 'Agents': [4056270]}}, {'PricingOptions': {'Price': 227243.14, 'Agents': [4056270]}}]

或者 按降序排列

newlist = sorted(data, key=lambda k: k['PricingOptions']["Price"], reverse=True) print(newlist) #[{'PricingOptions': {'Price': 227243.14, 'Agents': [4056270]}}, {'PricingOptions': {'Price': 51540.72, 'Agents': [4056270]}}] data = [ { "PricingOptions": { "Price": 51540.72, "Agents": [ 4056270 ] } }, { "PricingOptions": { "Price": 227243.14, "Agents": [ 4056270],} } ] newlist = sorted(data, key=lambda k: k['PricingOptions']["Price"]) print(newlist)

Output:

[{'PricingOptions': {'Price': 51540.72, 'Agents': [4056270]}}, {'PricingOptions': {'Price': 227243.14, 'Agents': [4056270]}}]

or in descending order

newlist = sorted(data, key=lambda k: k['PricingOptions']["Price"], reverse=True) print(newlist) #[{'PricingOptions': {'Price': 227243.14, 'Agents': [4056270]}}, {'PricingOptions': {'Price': 51540.72, 'Agents': [4056270]}}]

更多推荐

本文发布于:2023-08-07 18:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   中对   字典   列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!