在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]}}]更多推荐
发布评论