【问题标题】:How to print new line in nested list comprehension?如何在嵌套列表理解中打印新行?
【发布时间】:2019-12-17 09:41:48
【问题描述】:

我是 python 的新手。我想使用列表理解显示两个骰子的可能性。我得到了输出。但是当 y 达到 6(y==6) 时是否可以打印新行?

我的编码:

d1=(1,2,3,4,5,6) 
d2=(1,2,3,4,5,6) 
print([(x,y) for x in d1 for y in d2])

我的输出:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

我的期望输出:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), 
(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), 
(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), 
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), 
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), 
(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

【问题讨论】:

    标签: python python-3.x list python-2.7 list-comprehension


    【解决方案1】:

    打印有一个可选参数end 这通常是一个新行,但您可以对其进行修改,使其成为空格或任何您想要的。您可以使用它来重新创建所需的输出:

    d1=(1,2,3,4,5,6)
    d2=(1,2,3,4,5,6)
    # this is bad style and should be avoided because of side effects (see comments)
    ([print((x,y), end='\n' if y==len(d2) else ' ') for x in d1 for y in d2]) 
    

    没有副作用更好:

    for tup in [(x,y) for x in d1 for y in d2]:
        print(tup, end='\n' if tup[1]==len(d2) else ' ')
    

    输出:

    (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6)
    (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6)
    (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6)
    (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6)
    (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6)
    (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6)
    

    如果你真的想要一个真实的列表输出(带有所有特殊字符(“,”,“[”,“]”),那么你也可以做更多的工作:

    def print_nice_list(d1,d2):
        print("[",end="")
        for tup in [(x,y) for x in d1 for y in d2]:
            end = ", "
            if tup[1] == len(d2) and tup[0]==len(d1):
                end = "]"
            elif tup[1] == len(d2):
                end = ",\n"
            print(tup, end=end )
    
    print_nice_list(d1,d2) 
    

    输出:

    [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
    (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
    (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
    (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),
    (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
    (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
    

    编辑:根据 cmets 修改答案。

    【讨论】:

    • 谢谢。它现在工作。我现在正在学习列表理解。我仍然有一些疑问,代码的含义是“ print((x,y), end='\n' if y==6 else ' ') ”。因为当我学习列表理解时,它告诉表达将是第一位的。这里不一样。如果你有时间,请解释一下。
    • 列表推导构建了一个由 print 调用返回的 None 值列表,不幸的是,这不是 pythonic
    • 对副作用使用列表推导(([print((x,y), end='\n' if y==6 else ' ') for x in d1 for y in d2]) 被认为是不好的风格。
    • 另外,在print_nice_list() 中,您不应该对边界值(在这种情况下为6)进行硬编码,而是使用参数的长度(d1d2)。
    • @user3661367 我做的是不好的风格,但无论如何我都会向你解释。表达式是第一个,但整个表达式是 print((x,y), end='\n' if y==len(d2) else ' ') 并且在这个表达式中我有一个内联 if else 条件,它将改变end 参数是否需要换行。
    【解决方案2】:

    如果这只是为了可视化,你应该使用pprint

    >>> import pprint
    >>> lst = [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
    >>> pprint.pprint(lst, compact=True, width=50)
    [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
     (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
     (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
     (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),
     (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
     (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2012-11-26
      • 2021-07-22
      相关资源
      最近更新 更多