I have a list of dictionaries, like this:
ls_dict = [{'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'},
{'A': 65, 'B': 66, 'C': 67},
{'a': 97, 'b': 98, 'c': 99}]
Which produces this:
[{'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, {'A': 65, 'B': 66, 'C': 67}, {'a': 97, 'b': 98, 'c': 99}]
What I want is a dictionary of dictionaries where each dictionary is indexed like the following:
{"0": {'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, "1": {'A': 65, 'B': 66, 'C': 67}, 2: {'a': 97, 'b': 98, 'c': 99}}
The additional problem is that I need to convert the former to the latter, rather than adapting my sample creation code, as I just spent 15 hours downloading the data into the current (wrong) format.
Any suggestions? Thank you.
You can do with enumerate
,
In [75]: {str(index): lst for index, lst in enumerate(ls_dict)}
Out[75]:
{'0': {'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'},
'1': {'A': 65, 'B': 66, 'C': 67},
'2': {'a': 97, 'b': 98, 'c': 99}}
hello you need to do this whit for
keyList = ['0','1','2']
valueList = [{'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, {'A': 65, 'B': 66, 'C': 67}, {'a': 97, 'b': 98, 'c': 99}]
d = {}
for i in range(len(keyList)):
d[keyList[i]] = valueList[i]
print(d)
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.