python - 在列表的不同行上连续打印 5 个项目?

标签 python

我有一个未知数量的项目列表,比方说 26。 让我们说

list=['a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

如何打印:

abcde

fghij

klmno

pqrst

uvwxy

z

? 非常感谢。 尝试:

    start = 0
    for item in list:
        if start < 5:
            thefile.write("%s" % item)
            start = start + 5
        else:
            thefile.write("%s" % item)
            start = 0

最佳答案

for i, a in enumerate(A):
    print a, 
    if i % 5 == 4: 
        print "\n"

另一种选择,打印后的逗号表示没有换行符

关于python - 在列表的不同行上连续打印 5 个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451252/

相关文章:

python - scrapy 与负载请求

python - 如何在具有图例和次要 y 轴的同一图上绘制两个 Pandas 时间序列?

python - 用循环交换列表

python - 高效地将单行添加到 Pandas Series 或 DataFrame

python - TensorFlow 优化器是否通过分配来学习图中的梯度?

python - 将表单输入的行连接成一行

python - 从列表创建字典,每个元素作为键 :value pairs in python

python - 使用 scikit-learn OneHotEncoder 时如何处理分类数据中的缺失值 (NaN)?

python - AssertionError : Could not compute output Tensor

python - Excel 与 Python 交互