简体   繁体   中英

python create dictionary from list

I have this list in python

 ['Claudius II Gothicus', 'Antoninianus', None, ' Mediolanum']

It's possible to convert to a dictionary like this:

{
 "Herscher": "Claudius II Gothicus",
 "Fundort": "Antoninianus",
 "Material": "None",
"Art": "Mediolanum",

}
keys = ["Herscher", "Fundort", "Material", "Art"]
values = ['Claudius II Gothicus', 'Antoninianus', None, ' Mediolanum']
dict(zip(keys, values))
result_dict = {}
for i in range(len(your_list)):
    if i == 0:
        result_dict["Herscher"] = your_list[i]
    elif i == 1:
        result_dict["Fundort"] = your_list[i]
    elif i == 2:
        result_dict["Material"] = your_list[i]
    elif i == 3:
        result_dict["Art"] = your_list[i]
print(result_dict)

Output :

{
 "Herscher": "Claudius II Gothicus",
 "Fundort": "Antoninianus",
 "Material": "None",
"Art": "Mediolanum",

}
values = ['Claudius II Gothicus', 'Antoninianus', None, ' Mediolanum']
keys = ["Herscher", "Fundort", "Material", "Art"]
obj = {}
for idx,key in enumerate(keys):
    obj[key] = values[idx]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM