【问题标题】:Python: printing out contents of a dictionary horizontal with a for loopPython:使用for循环打印出水平方向的字典内容
【发布时间】:2017-06-27 16:03:02
【问题描述】:

我想水平打印字典的键,每隔三个键打印一个新行。我是这样写的:

fields= {1 : "not", 2 : "not", 3 : "not",
         4 : "not", 5 : "not", 6 : "not",
         7 : "not", 8 : "not", 9 : "not"} 
for i in fields:
print(i)
if i%3==0:
    print ("\n")

但是,输出只是水平的,每三个数字之间有两个换行符。

我想要的输出是这样的:

123

456

789

【问题讨论】:

  • 你的问题假设字典是有序的,这不是真的。
  • 所以订购 dict 会解决这个问题吗?因为我注意到以这种方式打印字符串时会导致相同的垂直输出
  • 您使用的是哪个版本的 Python,print(i) 应该每行打印一个,在末尾添加一个逗号以在同一行打印,并添加一个空格。将 print(i) 更改为 print(i), ;除此之外,是的,不能保证订购(但不能解决您的问题);在更深的层面上,“你想做什么?”你为什么需要这样打印的钥匙?坐标重要吗?这是保存该信息的正确数据结构吗?

标签: python dictionary printing


【解决方案1】:

这是你想要的吗?

fields= {1 : "not", 2 : "not", 3 : "not",
         4 : "not", 5 : "not", 6 : "not",
         7 : "not", 8 : "not", 9 : "not"}
for Key,values in fields.items():
    print(Key, end = "")#python3 , if in python2 use print item, instead.
    if Key%3==0:
        print ("\n")


123

456

789

【讨论】:

  • 很高兴能帮上忙~根据你的努力,稍微改变一下~
【解决方案2】:

如果你使用python 3,或者导入python 2的print函数,可以指定print的结束参数,像这样:print(i, end=""),字典键之间不会有换行符。 但由于字典键没有排序,它们将按“随机”顺序打印,为避免这种情况,制作一个键列表,并在打印前对其进行排序,如下所示:keys = sorted(dict.keys())

【讨论】:

  • 另外,您可以使用sys.stdout.write() 代替 print,它不会在其参数后附加任何结尾,但它只接受字符串对象
【解决方案3】:

print (i), 将为您解决问题。顺便说一句,您的代码缩进不正确。

fields= {1 : "not", 2 : "not", 3 : "not",
         4 : "not", 5 : "not", 6 : "not",
         7 : "not", 8 : "not", 9 : "not"} 
for i in fields:
  print (i),
  if i%3==0:
    print ("\n")`

【讨论】:

    【解决方案4】:

    虽然解决方案适用于给定的案例,但我想向您解释一些事情。 python 中的dictionary 未排序。在这种情况下,这很好,因为您使用简单的1,2,3... 键声明它的方式。您可能认为 dict 就是这样工作的。

    >>> a = {1 : "not", 2 : "not", 3 : "not",4 : "not", 5 : "not", 6 : "not",7 : "not", 8 : "not", 9 : "not"}
    >>> a
    {1: 'not', 2: 'not', 3: 'not', 4: 'not', 5: 'not', 6: 'not', 7: 'not', 8: 'not', 9: 'not'}
    

    很好,所以在这种情况下,所有解决方案都可以工作。但是,如果您有相同的想法并尝试以下dict的相同代码

    >>> a= {12 : "not", 21 : "not", 30 : "not",4 : "not", 15 : "not", 61 : "not",71 : "not", 8 : "not", 19 : "not"}
    

    输出的顺序会不同

    >>> a
    {19: 'not', 4: 'not', 21: 'not', 71: 'not', 8: 'not', 12: 'not', 61: 'not', 30: 'not', 15: 'not'}
    

    如果您尝试使用for keys in a: 进行迭代和打印,您会遇到很多问题。

    所以你想要想要的输出(dict 没有排序,所以不可预测)使用OrderedDict

    from collections import OrderedDict
    fields = [(1, 'not'), (2, 'not'), (3, 'not'),
              (4, 'not'), (5, 'not'), (6, 'not'),
              (7, 'not'), (8, 'not'), (9, 'not')]
    a = OrderedDict(fields)
    for i,key in enumerate(a):
        print(key,end='')
        i+=1
        if i%3==0:
            print()
    

    输出:

    123
    456
    789
    

    还应使用enumerate() 打印每三个值。不要依赖密钥。如果您更改密钥怎么办?

    i,key of enumerate(dictionary) 只给出0,key1,1,key2,2,key3,...等值。只给出成对的数字,项目就是这样。

    尝试更改密钥并使用此代码和其他代码,您会注意到您更改了多少,结果对于此解决方案将是相同的。而不是为了其他人。 记住你的代码应该是适应性强的。不应完全依赖于特定的输入集

    123
    456
    789
    

    另外注意如果您希望将它们分隔成换行符,只需 print() 即可。如果您希望它们之间有一个间隙(换行符),请使用 `print("\n")。

    123
    
    456
    
    789
    

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 2022-01-12
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多