my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
使用 python,我需要将列表显示为:
one, two, three
four, five, six
seven
我需要它是灵活的,因为列表会经常变化。
最佳答案
您可以使用石斑鱼 recipe来自 itertools:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
像这样使用:
for line in grouper(3, my_list):
print ', '.join(filter(None, line))
在线查看它:ideone
关于python - 在python中将列表输出为 "matrix"类型格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271639/