【问题标题】:How to print dictionary values line by line in Python?如何在 Python 中逐行打印字典值?
【发布时间】:2017-04-12 17:21:11
【问题描述】:

这是我的字典

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

我刚开始学字典,请帮忙

【问题讨论】:

    标签: python python-2.7 dictionary key


    【解决方案1】:
    # 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
    

    【讨论】:

      【解决方案2】:

      好的,如果您将字典称为 a,您可以这样做:

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

      如果你使用的是 python 2.X,你可以用 print 去掉括号

      【讨论】:

        【解决方案3】:

        您正在尝试打印字典中的所有值,其中值是列表。我们这样做的方式是:

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

        【讨论】:

        • 我会使用 'for key in my_dict.keys()' 但估计差别不大
        • 这绝对有助于提高可读性!另一种选择是使用for key, value in my_dict.iteritems()。我总是很难决定哪一个是正确的,我想这完全取决于情况!
        【解决方案4】:

        考虑到您对字典的输入是key:[list of values]。下面的代码应该能够实现你正在寻找的 -

        dictionary = {'a': [1,2], 'b': [3,4], 'c':[5,6,7,8]}
        for key,value in dictionary.items():
            print(key, ':', value)
        

        输出将是(键将不按特定顺序打印)-

        b : [3, 4]
        c : [5, 6, 7, 8]
        a : [1, 2]
        

        【讨论】:

          猜你喜欢
          • 2013-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-17
          相关资源
          最近更新 更多