I have a list in Python e.g.
我有一个Python列表,例如
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)
Will give the output as;
将输出作为;
["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.
注意:它必须在一行中。
8 个解决方案
#1
128
print ', '.join(names)
This, like it sounds, just takes all the elements of the list and joins them with ', '
.
这听起来像只需要列表中的所有元素并将它们与','连接起来。
#2
29
Here is a simple one.
这是一个简单的。
names = ["Sam", "Peter", "James", "Julian", "Ann"]
print(*names)
the star unpacks the list and return every element in the list.
星形解包列表并返回列表中的每个元素。
#3
28
General solution, works on arrays of non-strings:
一般解决方案,适用于非字符串数组:
>>> print str(names)[1:-1]
'Sam', 'Peter', 'James', 'Julian', 'Ann'
#4
11
If the input array is Integer type then you need to first convert array into string type array and then use join
method for joining with ,
or space whatever you want. e.g:
如果输入数组是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
>>>
Direct using of join which will join the integer and string will throw error as show above.
直接使用将连接整数和字符串的连接将抛出错误,如上所示。
#5
8
This is what you need
这就是你需要的
", ".join(names)
#6
8
There are two answers , First is use 'sep' setting
有两个答案,第一个是使用'sep'设置
>>> print(*names, sep = ', ')
The other is below
另一个在下面
>>> print(', '.join(names))
#7
3
You need to loop through the list and use end=" "
to keep it on one line
您需要遍历列表并使用end =“”将其保持在一行
names = ["Sam", "Peter", "James", "Julian", "Ann"]
index=0
for name in names:
print(names[index], end=", ")
index += 1
#8
0
I don't know if this is efficient as others but simple logic always works:
我不知道这是否有效,但简单的逻辑始终有效:
import sys
name = ["Sam", "Peter", "James", "Julian", "Ann"]
for i in range(0, len(names)):
sys.stdout.write(names[i])
if i != len(names)-1:
sys.stdout.write(", ")
Output:
输出:
Sam, Peter, James, Julian, Ann
山姆,彼得,詹姆斯,朱利安,安
#1
128
print ', '.join(names)
This, like it sounds, just takes all the elements of the list and joins them with ', '
.
这听起来像只需要列表中的所有元素并将它们与','连接起来。
#2
29
Here is a simple one.
这是一个简单的。
names = ["Sam", "Peter", "James", "Julian", "Ann"]
print(*names)
the star unpacks the list and return every element in the list.
星形解包列表并返回列表中的每个元素。
#3
28
General solution, works on arrays of non-strings:
一般解决方案,适用于非字符串数组:
>>> print str(names)[1:-1]
'Sam', 'Peter', 'James', 'Julian', 'Ann'
#4
11
If the input array is Integer type then you need to first convert array into string type array and then use join
method for joining with ,
or space whatever you want. e.g:
如果输入数组是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
>>>
Direct using of join which will join the integer and string will throw error as show above.
直接使用将连接整数和字符串的连接将抛出错误,如上所示。
#5
8
This is what you need
这就是你需要的
", ".join(names)
#6
8
There are two answers , First is use 'sep' setting
有两个答案,第一个是使用'sep'设置
>>> print(*names, sep = ', ')
The other is below
另一个在下面
>>> print(', '.join(names))
#7
3
You need to loop through the list and use end=" "
to keep it on one line
您需要遍历列表并使用end =“”将其保持在一行
names = ["Sam", "Peter", "James", "Julian", "Ann"]
index=0
for name in names:
print(names[index], end=", ")
index += 1
#8
0
I don't know if this is efficient as others but simple logic always works:
我不知道这是否有效,但简单的逻辑始终有效:
import sys
name = ["Sam", "Peter", "James", "Julian", "Ann"]
for i in range(0, len(names)):
sys.stdout.write(names[i])
if i != len(names)-1:
sys.stdout.write(", ")
Output:
输出:
Sam, Peter, James, Julian, Ann
山姆,彼得,詹姆斯,朱利安,安