如何在Python中逐行打印字典值?

时间:2017-04-12 17:21:12

标签: python python-2.7 dictionary key

这是我的字典

 {'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 
....

我刚开始学习字典请帮忙

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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. 上的列表进行迭代
  5. 打印每个值。

答案 2 :(得分:0)

好的,如果你打电话给你的dictionnary,你可以这样做:

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

如果您使用的是python 2.X,则可以使用print

删除括号