如何使用括号,逗号和无引号的整数打印列表? [重复]

时间:2021-11-15 02:05:33

This question already has an answer here:

这个问题在这里已有答案:

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?

我不想要括号,逗号或引号。该怎么办?

5 个解决方案

#1


39  

If you're using Python 3, or appropriate Python 2.x version with from __future__ import print_function then:

如果您正在使用Python 3或适当的Python 2.x版本以及__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))

#2


8  

Try this:

尝试这个:

print("".join(str(x) for x in This))

#3


5  

Using .format from Python 2.6 and higher.

使用Python 2.6及更高版本的.format。

>>> print '{}{}{}{}'.format(*[7,7,7,7])
7777
>>> data = [7, 7, 7, 7] * 3
>>> print ('{}'*len(data)).format(*data)
777777777777777777777777

#4


3  

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])))

#5


2  

Something like this should do it:

这样的事情应该这样做:

for element in list_:
   sys.stdout.write(str(element))

#1


39  

If you're using Python 3, or appropriate Python 2.x version with from __future__ import print_function then:

如果您正在使用Python 3或适当的Python 2.x版本以及__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))

#2


8  

Try this:

尝试这个:

print("".join(str(x) for x in This))

#3


5  

Using .format from Python 2.6 and higher.

使用Python 2.6及更高版本的.format。

>>> print '{}{}{}{}'.format(*[7,7,7,7])
7777
>>> data = [7, 7, 7, 7] * 3
>>> print ('{}'*len(data)).format(*data)
777777777777777777777777

#4


3  

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])))

#5


2  

Something like this should do it:

这样的事情应该这样做:

for element in list_:
   sys.stdout.write(str(element))