如何在字典列表中打印字典值?

时间:2015-10-21 01:04:12

标签: python list dictionary

我有一个字典列表,我想从每个字典中打印某些键:

dict_results=[{'terms':['hi','bye'],'title':'01234def','text':'hello, how are you?'},{'terms':...}...]

这不起作用:

for item in dict_results:
    print "{0},{1}".format(item[0]['title'],item[0]['text'])

键错误:0

2 个答案:

答案 0 :(得分:3)

循环浏览list,然后访问每个词典的键:

>>> dict_results=[{'terms':['hi','bye'],'title':'01234def','text':'hello, how are you?'},
...               {'terms':0, 'title':'hello', 'text':'world'}]
>>> for d in dict_results:
...     print(d['title'], d['text'])
...
01234def hello, how are you?
hello world

答案 1 :(得分:0)

dict_results=[{'terms':['hi','bye'],'title':'01234def','text':'hello, how are you?'}]

for item in dict_results:

    print("{} {} {}".format(item['terms'], item['title'],item['text']))

    ['hi', 'bye'] 01234def hello, how are you?