英文:
sort dictionary alphabetically by key in odoo 13
问题
我已经创建了一个字典,根据客户的要求收集订单,已经下单的产品,我需要按客户的字母顺序对其进行排序,以在qweb报告中使用,到目前为止,我尝试过的任何方法都没有奏效。有什么想法?
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
#...
}
reparto_data2 = {}
for i in sorted(reparto_data[key]):
reparto_data2[i] = reparto_data[i]
英文:
I have built a dictionary to collect orders according to the client, the products that have been ordered and I need to order it alphabetically by client to use it in a qweb report, nothing I have tried so far has worked for me. Any ideas?
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
#...
}
reparto_data2 = {}
for i in sorted(reparto_data[key]):
reparto_data2[i] = reparto_data[i]
答案1
得分: 1
需要对内部的字典进行排序。
因此,遍历外部字典的键和值,并对每个值(内部字典)进行排序。
from pprint import pprint
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
}
def sort_dict(d):
return {k: d[k] for k in sorted(d.keys())}
reparto_data2 = {k: sort_dict(v) for k,v in reparto_data.items()}
pprint(reparto_data2, width=1)
输出:
{'Cliente 1': {'Bomba crema': 8,
'Pastel manzana': 12},
'Cliente 2': {'Bombon chocolate': 8,
'Cake calabaza': 18}}
这是对内部字典进行排序后的输出。
英文:
You need to sort the inner dictionaries.
So, iterate across the keys and values of the outer dict, and for each value (which is an inner dict), sort it.
from pprint import pprint
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
}
def sort_dict(d):
return {k: d[k] for k in sorted(d.keys())}
reparto_data2 = {k: sort_dict(v) for k,v in reparto_data.items()}
pprint(reparto_data2, width=1)
Output
{'Cliente 1': {'Bomba crema': 8,
'Pastel manzana': 12},
'Cliente 2': {'Bombon chocolate': 8,
'Cake calabaza': 18}}
答案2
得分: 0
你可以尝试这个方法。
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
}
sorted_clients = sorted(reparto_data.keys(), key=lambda x: x.lower())
reparto_data2 = {i: reparto_data[i] for i in sorted_clients}
输出结果为:
{'Cliente 1': {'Pastel manzana': 12, 'Bomba crema': 8}, 'Cliente 2': {'Cake calabaza': 18, 'Bombon chocolate': 8}}
如果要使用自己的代码,请用以下代码替换这部分:
reparto_data2 = {i: reparto_data[i] for i in sorted(reparto_data[key])}
英文:
You can try this too.
reparto_data = {
'Cliente 1': {
'Pastel manzana': 12,
'Bomba crema': 8,
},
'Cliente 2': {
'Cake calabaza': 18,
'Bombon chocolate': 8,
},
}
sorted_clients = sorted(reparto_data.keys(), key=lambda x:x.lower())
reparto_data2 = {i: reparto_data[i] for i in sorted_clients}
Output
{'Cliente 1': {'Pastel manzana': 12, 'Bomba crema': 8}, 'Cliente 2': {'Cake calabaza': 18, 'Bombon chocolate': 8}}
If want to use your own code just replace this with your loop
reparto_data2 = {i: reparto_data[i] for i in sorted(reparto_data[key])}
答案3
得分: 0
感谢你的帮助,但我提出问题的方式是错误的,因为字典包含了对象。
对我有效的代码如下:
reparto_data2 = sorted(reparto_data.items(), key=lambda x: x[0].lower())
英文:
Thanks for your help but I have posed the question wrong since the dictionary contains objects.
The code that has worked for me is the following:
reparto_data2 = sorted(reparto_data.items(), key=lambda x: x[0].lower())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论