简体   繁体   English

Python3列表打印不带空格

[英]Python3 list printing without spaces

i hvae this code here. 我在这里这段代码。 i want to print a list without spaces. 我想打印一个没有空格的列表。 here l is a list with 3 elements that i trying to print: 这里l是3倍,也就是说,我想打印的清单:

>>> l=[]
>>> l.append(5)
>>> l.append(6)
>>> l.append(7)
>>> print(l)

i get in the output: 我进入输出:

[5, 6, 7]

but i wanna get: 但我想得到:

[5,6,7]

what should i add to the syntax in append or in print to print the list without spaces 我应该在appendprintappend什么语法以打印没有空格的列表

您需要使用类似:

print('[{0}]'.format(','.join(map(str, l))))

如果结果太大,可以修改:

print(repr(l).replace(' ', ''))

加入列表元素。

print("[" + ",".join([str(i) for i in l]) + "]")

You could convert it to a string and then replace the spaces. 您可以将其转换为字符串,然后替换空格。 Eg: 例如:

print ("{}".format(l)).replace(' ', '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM