本文介绍了单行不带括号的打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Python中有一个列表例如
I have a list in Pythone.g.
names = ["Sam", "Peter", "James", "Julian", "Ann"]
我要在一行中打印出不带普通"[]
I want to print the array in a single line without the normal " []
names = ["Sam", "Peter", "James", "Julian", "Ann"]
print (names)
将给出的输出为;
["Sam", "Peter", "James", "Julian", "Ann"]
那不是我想要的格式,而是我希望它像这样;
That is not the format I want instead I want it to be like this;
Sam, Peter, James, Julian, Ann
注意:它必须在一行中.
Note: It must be in a single row.
推荐答案
print(', '.join(names))
听起来,这只是获取列表中的所有元素,然后将它们与', '
结合在一起.
This, like it sounds, just takes all the elements of the list and joins them with ', '
.
这篇关于单行不带括号的打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!