-1

我有一个包含许多字典的列表,我需要获取每个字典的键和值:

[{'keys1.1' = 'value1.1','key1.2'='values1.2'},{'keys2.1'='value2.1'},{'key3.1'='value3.1'}]

我怎样才能得到这些键和值?

4

1 回答 1

0

您需要遍历 a 中的dicts 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])
于 2013-03-07T16:56:34.963 回答