如何打印不带括号但带引号的数组?

时间:2019-02-21 14:49:29

标签: python python-3.x python-2.7 jupyter-notebook

如何打印不带括号但带引号的数组?

我有:

a = ['1','2','3','4','5']
' '.join(map(str, a))

我收到的结果:

1, 2, 3, 4, 5

预期结果(想要):

'1','2','3','4','5'

(带逗号和引号)

5 个答案:

答案 0 :(得分:3)

这是因为当您执行' '.join(...)时,会得到一个大字符串而不是它们的列表。如果要保留引号,则需要将其重新添加回自己:

>>> print(', '.join("'{}'".format(x) for x in a))
'1', '2', '3', '4', '5'

答案 1 :(得分:3)

执行以下操作:

a = ['1','2','3','4','5']
print(str(a).strip('[]'))

答案 2 :(得分:0)

a = ['1','2','3','4','5'] 
print([str(item) for item in a])

我喜欢这样。

答案 3 :(得分:0)

", ".join("'{}'".format(x) for x in a)

答案 4 :(得分:0)

试图用老式的弦建解决方案赶上火车

a = ['1','2','3','4','5']
out_string=''
for i in a:
    out_string+="'"+ i +"'"+', '
print(out_string.rstrip(', ')) # '1', '2', '3', '4', '5'