2017-04-12 41 views
0

這是我的字典如何在Python中逐行打印字典值?

{'www.pic2fly.com/Weather+Of+Bahawalpur.html': ['https://tse1.mm.bing.net/th? 
id=OIP.RsEykDoK1xxg2zivHYW7WwEsC5&pid=Api'], 'panoramio.com/photo/84118355': 
['https://tse2.mm.bing.net/th?id=OIP.1683LeSgJHoFhxX-tKhGSAEsDh&pid=Api'], 'nativepakistan.com/photos-of-bahawalpur': ['https://tse3.mm.bing.net/th?id=OIP.wS0pep46eEsGSSY39RNxLQEsDQ&pid=Api', 'https://tse2.mm.bing.net/th?id=OIP.Zn5zIzevX8HqUnwCyRZ3QwEsDJ&pid=Api', 'https://tse1.mm.bing.net/th?id=OIP.671-BIumri8_oRAQRLd-ggEsDJ&pid=Api', 'https://tse3.mm.bing.net/th?id=OIP.0dShzaFVWKBtTSxnOFHyaAEsDI&pid=Api', 'https://tse1.mm.bing.net/th?id=OIP.LXLVj2t-dawxlMcJp1GBTAEsDV&pid=Api', 'https://tse4.mm.bing.net/th?id=OIP.Z0-Y7RzbFM43N6YFNevsTQEsDL&pid=Api', 'https://tse1.mm.bing.net/th?id=OIP.AYjL8nxCNUE-HvEiifvqWQEsCc&pid=Api', 'https://tse4.mm.bing.net/th?id=OIP.Iy9NYKksUM3TSpgmOc0-JgDZEs&pid=Api', 'https://tse3.mm.bing.net/th?id=OIP.NHdCNr5g2CYn3L6wP77BygEsDg&pid=Api']} 

使用for循環我打印值這樣這個

for key in res.iterkeys(): 
    print key 

for value in res.itervalues(): 
    print value 

我需要這個作爲輸出

www.pic2fly.com/Weather+Of+Bahawalpur.html 

https://tse3.mm.bing.net/th?id=OIP.wS0pep46eEsGSSY39RNxLQEsDQ&pid=Api 

panoramio.com/photo/84118355 

https://tse2.mm.bing.net/th?id=OIP.Zn5zIzevX8HqUnwCyRZ3QwEsDJ&pid=Api 

nativepakistan.com/photos-of-bahawalpur 

https://tse1.mm.bing.net/th?id=OIP.671-BIumri8_oRAQRLd-ggEsDJ&pid=Api 
https://tse3.mm.bing.net/th?id=OIP.0dShzaFVWKBtTSxnOFHyaAEsDI&pid=Api 
https://tse1.mm.bing.net/th?id=OIP.LXLVj2t-dawxlMcJp1GBTAEsDV&pid=Api 
.... 

我剛開始學習詞典,請幫助

回答

0

您正在嘗試打印所有值一個字典,其中的值是列表。我們這樣做的方式是:

for key in my_dict: 
    print key 
    for list_value in my_dict[key]: 
     print list_value 
  1. 遍歷鍵在字典
  2. 打印鍵
  3. 遍歷每個這些值的my_dict[key]
  4. 打印發現列表中。
+1

我會用'對於my_dict.keys()中的關鍵字'但是沒有太大的區別 –

+0

這絕對有助於可讀性!另一種選擇是使用'for key,my_dict.iteritems()'中的值。我總是很難決定哪一個是正確的,我想這一切都取決於情況! –

2
# iterate the dictionary with the key and val at the same time 
for key, val in res.items(): 
    # print the key 
    print key 
    for x in val: 
    # iterate the list of the val and print all items 
     print x 
0

好了,所以如果你打電話給dictionnary一個你可以做:

for i in a: 
    print(i) 
    for j in a[i]: 
     print(j) 

如果你使用python 2.X您可以用打印去掉括號