gpt4 book ai didi

python - 如何将列表格式化为具有一定数量项目的行?

转载 作者:行者123 更新时间:2023-11-30 21:59:25 29 4
gpt4 key购买 nike

我在将列表格式化为格式化输出时遇到问题,每行包含五个元素,但我陷入困境。

words = ["letter", "good", "course", "land", "car", "tea", "speaker",\
"music", "length", "apple", "cash", "floor", "dance", "rice",\
"bow", "peach", "cook", "hot", "none", "word", "happy", "apple",\
"monitor", "light", "access"]

输出:

letter good course land car
tea speaker music length apple
cash floor dance rice bow
peach cook hot none word
happy apple monitor light access

最佳答案

试试这个:

>>> for i in range(0, len(words), 5):
... print ' '.join(words[i:(i+5)])
...
letter good course land car
tea speaker music length apple
cash floor dance rice bow
peach cook hot none word
happy apple monitor light access

关于python - 如何将列表格式化为具有一定数量项目的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600686/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com