Python - 打印行

2024-05-13 21:13:10 发布

您现在位置:Python中文网/ 问答频道 /正文

说我有一张单子

food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape']

我将如何打印包含4列的行中的列表,使其看起来像:

apple pear tomato bean   
carrot grape

Tags: apple列表foodlist单子pearbeantomato
3条回答

试试这个

food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape']
size = 4
g = (food_list[i:i+size] for i in xrange(0, len(food_list), size))
for i in g:
    print i
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape']
index = 0
for each_food in food_list:
    if index < 3:
            print each_food,
            index += 1
    else:
            print each_food
            index = 0
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape']
for i in xrange(0, len(food_list), 4):
    print '\t'.join(food_list[i:i+4])

相关问题 更多 >