小编use*_*396的帖子

如何在python中订购我的字典?

可能重复:
在Python中,如何按排序顺序迭代字典?

我需要字典方面的帮助.我有两个词典,我想在这两个词典中添加相同键的值.我需要列出总和具有相同键的值的列表.我做了列表,但是在完成所有计算之后添加了他们的键是两个词典中唯一的那些值.我的意思是:

dectionary1= {8: 2, 0: 6, 2: 5, 3: 34}  
dectionary2= {8: 6, 1: 2, 3: 2}
Run Code Online (Sandbox Code Playgroud)

我的清单必须是:

summing= [6, 2, 5, 36, 8]
Run Code Online (Sandbox Code Playgroud)

因为它将取0并检查在行为2中是否有0,然后它将取得1(不是2)并检查是否在行动1中找到它以便订购列表.

我到目前为止得到了这个:

summing=[8, 6, 5, 36, 2]
Run Code Online (Sandbox Code Playgroud)

这里最初需要键(8)而不是(0)!! 我希望它有条不紊.

看到我的代码,到目前为止我得到的:

dic1= {8: 2, 0: 6, 2: 5, 3: 34}  
dic2= {8: 6, 1: 2, 3: 2}
p=[]
for x in dic1:
    if x in dic2:
        g=dic1[x]+dic2[x]
        p=p+[g]
    else:
        p=p+[dic1[x]]
for m in dic2:
    if m in dic1:
        p=p
    else:
        p=p+[dic2[m]]
Run Code Online (Sandbox Code Playgroud)

我想如果我可以让字典按顺序递增,那将会更容易,但是如何?

我的python是Wing IDE 3.2 …

python

1
推荐指数
1
解决办法
307
查看次数

标签 统计

python ×1