我有一个Python中的列表
如。
names = ["Sam", "Peter", "James", "Julian", "Ann"]
我想打印的数组在单行没有正常的" []
names = ["Sam", "Peter", "James", "Julian", "Ann"]
print (names)
将给出输出为;
["Sam", "Peter", "James", "Julian", "Ann"]
这不是我想要的格式,而是这样的;
Sam, Peter, James, Julian, Ann
注意:必须为单行。
如果输入数组是Integer类型,那么您需要首先将数组转换为字符串类型的数组,然后使用join方法来连接或空格。例句:
>>> arr = [1, 2, 4, 3]
>>> print(", " . join(arr))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> sarr = [str(a) for a in arr]
>>> print(", " . join(sarr))
1, 2, 4, 3
>>>
直接使用join将连接整数和字符串将抛出错误如上所示。
如果输入数组是Integer类型,那么您需要首先将数组转换为字符串类型的数组,然后使用join方法来连接或空格。例句:
>>> arr = [1, 2, 4, 3]
>>> print(", " . join(arr))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> sarr = [str(a) for a in arr]
>>> print(", " . join(sarr))
1, 2, 4, 3
>>>
直接使用join将连接整数和字符串将抛出错误如上所示。