簡體   English   中英

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

[英]how to print dictionary values in a list of dictionaries?

我有一個字典列表,我想從每個字典中打印某些鍵:

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

遍歷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
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?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM