【问题标题】:How to display a list in python like this? [duplicate]如何像这样在python中显示列表? [复制]
【发布时间】:2020-09-30 09:55:13
【问题描述】:

名单如下:

seats = []
seats.append([0,0,1,1,0,1,1,1])
seats.append([0,1,1,0,0,1,0,1])
seats.append([1,0,0,1,0,1,1,0])
seats.append([0,1,1,1,0,0,0,1])
seats.append([0,0,1,1,0,1,0,0])
seats.append([1,0,1,1,0,0,1,1])

我希望输出使每一行显示在不同的行上,如下所示:

0,0,1,1,0,1,1,1
0,1,1,0,0,1,0,1
1,0,0,1,0,1,1,0
0,1,1,1,0,0,0,1
0,0,1,1,0,1,0,0
1,0,1,1,0,0,1,1

我尝试过print(seats),但它会将所有行连续打印在一行上。 我在 11 年级时对 Python 没有太多经验,而且我刚刚开始学习它。

我不知道接下来该尝试什么,所以如果有人冷酷地帮助我,那真是太棒了。

【问题讨论】:

  • for row in seats: print(",".join(str(i) for i in row))

标签: python


【解决方案1】:
for row in seats:
    print(row)

如果您不想在输出中使用括号:

for row in seats:
    print(", ".join(str(s) for s in row))

【讨论】:

    【解决方案2】:
    for row in seats:
    print(str(row).replace("[","").replace("]",""))
    

    期望的输出

    0,1,1,0,0,1,0,1
    1,0,0,1,0,1,1,0
    0,1,1,1,0,0,0,1
    0,0,1,1,0,1,0,0
    1,0,1,1,0,0,1,1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 2020-12-16
      • 1970-01-01
      • 2016-10-21
      • 2018-12-01
      • 2013-01-12
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多