listtoordereddict 多列表转化为字典

时间:2022-04-09 06:44:51
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 import ordereddict
 5 
 6 list1=['jim','marry','terrence']
 7 list2=[13,15,18]
 8 
12 def unionListToDict(list1, list2):
13     result = ordereddict.OrderedDict()
14     for i in range(len(list1)):
15         #print list1[i],list2[i]
16         result[list1[i]] = list2[i]
17     return result
18 
19 
20 def dictToFile(dict):
21     fileName="-".join(dict.keys())+".dict"
22     fout=open(fileName,"w")
23     fout.write(str(dict1))
24     fout.close();
25 
26 
27 dict1=unionListToDict(list1,list2)
28 print dict1
29 # print "-".join(dict1.keys())
30 dictToFile(dict1)
31 
32 #输出为
33 OrderedDict([('jim', 13), ('marry', 15), ('terrence', 18)])