Cha*_*iri 2 python dictionary list
a_lst = list()
b_lst = list()
tmp = list()
c_dct = dict()
while True:
a = raw_input("a=")
a_lst.append(a)
b = raw_input("b=")
b_lst.append(b)
if a == "end":
break
a_lst.remove('end')
print a_lst
print b_lst
for i in range(len(a_lst)):
c_dct[a_lst[i]] = b_lst[i]
print c_dct
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我组合了两个列表来创建字典.在结果中,字典与输入的位置不同.例如,
c_dct = {'q': 'w', 'e': 'r', 'o': 'p', '1': '2', '3': '4', '5': '6', 't': 'y', '7': '8', '9': '0', 'u': 'i'}
Run Code Online (Sandbox Code Playgroud)
代替
c_dct = {'1': '2', '3': '4', '5': '6','7': '8', '9': '0','q': 'w', 'e': 'r','t': 'y','u': 'i','o': 'p'}
Run Code Online (Sandbox Code Playgroud)
代码怎么了?如何解决这个问题?非常感谢你!
字典默认情况下不维护顺序.如果您需要有序字典,可以使用以下内容:
from collections import OrderedDict
Run Code Online (Sandbox Code Playgroud)
看一下文档
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |