Skip to main content
 首页 » 编程设计

python之将元素附加到python中的字典

2025年05月04日1498°冷暖

dictionary = {} 
 
tabl = [('maint_id', 1003L), ('type', 'DB'), ('number', '102')] 
for i in tabl: 
     dictionary{i[0]:i[1]} 
 
 
print dictionary 

它只给我字典中的最后一个键值对。它正在覆盖所有元素。这段代码有什么问题..任何帮助

请您参考如下方法:

这更容易:

dictionary = dict(tabl) 

如果顺序很重要:

from collections import OrderedDict 
dictionary = OrderedDict(tabl)