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

2024-09-30 00:30:51 发布

您现在位置: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循环,我打印如下值

^{pr2}$

我需要这个作为输出

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

我刚开始学字典请帮忙


Tags: httpscomapiidnet字典wwwpid
3条回答

好吧,所以如果你把你的字典叫做a,你可以:

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

如果您使用的是python2.X,则可以在打印时删除括号

# 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

您试图打印字典中的所有值,其中列出了值。我们的方法是:

for key in my_dict:
    print key
    for list_value in my_dict[key]:
        print list_value
  1. 迭代字典中的键
  2. 打印钥匙
  3. 迭代在my_dict[key]处找到的列表
  4. 打印每个值。在

相关问题 更多 >

    热门问题