以设定长度的行打印列表(Python)

时间:2021-09-23 21:42:02

I'm fairly new to Python and having a hard time figuring out how to print a list in rows given these circumstances:

我对Python很陌生,并且很难在这些情况下弄清楚如何在行中打印列表:

The list starts out empty and the computer generates random 2-digit integer entries, which are then appended to the list. Printing the list is easy enough, either in a single column or in a single line using

列表开始为空,计算机生成随机的2位整数条目,然后将其附加到列表中。打印列表非常简单,可以使用单列或单行打印

for i in list:
    print(i, end = "")

but what I want to do is print in rows of 4, ending the list with whatever the remainder is (e.g. a row of 3), so that the outcome would look something like this:

但我想要做的是打印4行,以剩余的为止结束列表(例如3行),结果看起来像这样:

81  27  19  55
99  45  32  90
67  83  20  72
12  86  21

What is the best and most straightforward way to do this?

最好和最直接的方法是什么?

Edit: I forgot to clarify that I'm working in Python 3. Sorry about that.

编辑:我忘了澄清我在Python 3工作。抱歉。

5 个解决方案

#1


3  

There many ways to do this, as others have shown. One more:

正如其他人所表明的,有很多方法可以做到这一点。多一个:

for i in range(0, len(mylist), 4):
    print(*mylist[i:i+4], sep=' ')

#2


2  

You could use the modulus operator to recognize when you're on the 4th column and change the end value to print.

您可以使用模数运算符来识别第4列的时间并更改结束值以进行打印。

for i, val in enumerate(list):
    end = '\n' if (i + 1) % 4 == 0 else '  '
    print(val, end=end)

#3


1  

Just add a statement to drop in a newline every 4th item:

只需添加一个语句,每隔4个项目放入一个换行符:

count = 1
for i in list:
    print(i, end = "")
    count += 1
    if count % 4 == 0:
        print('\n')

If you like indexing better:

如果你更喜欢索引:

for i in len(list):
    print(list[i], end = "")
    if i % 4 == 0:
        print('\n')

Does that solve the problem for you?

这能为你解决问题吗?

#4


1  

for ind, val in enumerate(yourList):
    if ind != 0 and ind % 4 == 0:
        print('\n')
    print(val, end=' ')

#5


0  

If this is your starting list of numbers:

如果这是您的起始数字列表:

numbers = [81, 27, 19, 55, 99, 45, 32, 90, 67, 83, 20, 72, 12, 86, 21]

I would first group them into separate lists:

我首先将它们分成不同的列表:

grouped_numbers = [numbers[i:i+4] for i in range(0,len(numbers),4)]

Then I would print out each group with a blank space in between them using the join function

然后我会使用join函数打印出每个组之间有一个空格

for group_of_numbers in grouped_numbers:
    print (" ".join(map(str,group_of_numbers)))

#1


3  

There many ways to do this, as others have shown. One more:

正如其他人所表明的,有很多方法可以做到这一点。多一个:

for i in range(0, len(mylist), 4):
    print(*mylist[i:i+4], sep=' ')

#2


2  

You could use the modulus operator to recognize when you're on the 4th column and change the end value to print.

您可以使用模数运算符来识别第4列的时间并更改结束值以进行打印。

for i, val in enumerate(list):
    end = '\n' if (i + 1) % 4 == 0 else '  '
    print(val, end=end)

#3


1  

Just add a statement to drop in a newline every 4th item:

只需添加一个语句,每隔4个项目放入一个换行符:

count = 1
for i in list:
    print(i, end = "")
    count += 1
    if count % 4 == 0:
        print('\n')

If you like indexing better:

如果你更喜欢索引:

for i in len(list):
    print(list[i], end = "")
    if i % 4 == 0:
        print('\n')

Does that solve the problem for you?

这能为你解决问题吗?

#4


1  

for ind, val in enumerate(yourList):
    if ind != 0 and ind % 4 == 0:
        print('\n')
    print(val, end=' ')

#5


0  

If this is your starting list of numbers:

如果这是您的起始数字列表:

numbers = [81, 27, 19, 55, 99, 45, 32, 90, 67, 83, 20, 72, 12, 86, 21]

I would first group them into separate lists:

我首先将它们分成不同的列表:

grouped_numbers = [numbers[i:i+4] for i in range(0,len(numbers),4)]

Then I would print out each group with a blank space in between them using the join function

然后我会使用join函数打印出每个组之间有一个空格

for group_of_numbers in grouped_numbers:
    print (" ".join(map(str,group_of_numbers)))