我有一个包含许多字典的列表,我需要获取每个字典的键和值:
[{'keys1.1' = 'value1.1','key1.2'='values1.2'},{'keys2.1'='value2.1'},{'key3.1'='value3.1'}]
我怎样才能得到这些键和值?
我有一个包含许多字典的列表,我需要获取每个字典的键和值:
[{'keys1.1' = 'value1.1','key1.2'='values1.2'},{'keys2.1'='value2.1'},{'key3.1'='value3.1'}]
我怎样才能得到这些键和值?
您需要遍历 a 中的dict
s for-loop
:
list_of_dicts = [{'keys1.1' : 'value1.1',
'key1.2':'values1.2'},
{'keys2.1':'value2.1'},
{'key3.1':'value3.1'}]
for item in list_of_dicts:
for key, value in item.iteritems()
print key, value
print "The key is {0} with a value of {1}".format(key, value)
#or
for item in list_of_dicts:
for key in item.keys():
print item[key]
print "The key is {0} with a value of {1}".format(key, item[key])