如何在每行列表中打印X个项目 [英] How to print X items in a list per line

查看:91
本文介绍了如何在每行列表中打印X个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串列表,我希望能够每行打印该列表中的4个项目,并且它们之间只有一个空格,直到列表用完为止.有任何想法吗?谢谢

Given a list of strings I want to be able to print 4 items of that list per line with one space between them until the list is exhausted. Any ideas? Thanks

示例:

ListA = ['1', '2', '3', '4', '5', '6']

鉴于此列表,我希望输出为:

Given this list I would like my output to be:

1 2 3 4
5 6

推荐答案

您可以执行以下操作:

for i,item in enumerate(listA):
    if (i+1)%4 == 0:
        print(item)
    else:
        print(item,end=' ')

这篇关于如何在每行列表中打印X个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆