Python 如何打印没有括号、逗号和引号的整数列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17757450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print a list with integers without the brackets, commas and no quotes?
提问by Doug
This is a list of Integers and this is how they are printing:
这是一个整数列表,这就是它们的打印方式:
[7, 7, 7, 7]
I want them to simply print like this:
我希望他们像这样简单地打印:
7777
I don't want brackets, commas or quotes. What to do?
我不想要括号、逗号或引号。该怎么办?
回答by zwol
Try this:
尝试这个:
print("".join(str(x) for x in This))
回答by dstromberg
Something like this should do it:
像这样的事情应该这样做:
for element in list_:
sys.stdout.write(str(element))
回答by jh314
You can convert it to a string, and then to an int:
您可以将其转换为字符串,然后转换为 int:
print(int("".join(str(x) for x in [7,7,7,7])))
回答by Jon Clements
If you're using Python 3, or appropriate Python 2.x version with from __future__ import print_function
then:
如果您使用的是 Python 3 或适当的 Python 2.x 版本,from __future__ import print_function
则:
data = [7, 7, 7, 7]
print(*data, sep='')
Otherwise, you'll need to convert to string and print:
否则,您需要转换为字符串并打印:
print ''.join(map(str, data))
回答by dansalmo
Using .format
from Python 2.6 and higher:
使用.format
在Python 2.6和更高版本:
>>> print '{}{}{}{}'.format(*[7,7,7,7])
7777
>>> data = [7, 7, 7, 7] * 3
>>> print ('{}'*len(data)).format(*data)
777777777777777777777777
For Python 3:
对于 Python 3:
>>> print(('{}'*len(data)).format(*data))
777777777777777777777777