gpt4 book ai didi

python - python中的打印格式

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:42 25 4
gpt4 key购买 nike

我有一个问题纯粹是想改进我的编码。建议我有一个列表:

a = [1, 2, 3]

我想打印如下结果:

Path is: 1 --> 2 --> 3

我的代码工作得很好:

text = ''
for j in range(len(a)):
text += str(a[j])
if j < len(a) - 1:
text = text + ' --> '
print('Path is:', text)

我想知道,有没有更好的方法呢?谢谢。

最佳答案

使用str.join:

' --> '.join(map(str, a))

或者如果 a 已经只包含字符串:

' --> '.join(a)

关于python - python中的打印格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428712/

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