简体   繁体   English

如何在不带括号的情况下打印列表

[英]How to print a list without brackets

I have a list with float values. 我有一个带有浮点值的列表。 I want to remove the the brackets from the list. 我想从列表中删除括号。

Floatlist = [14.715258933890,10.215953824,14.8171645397,10.2458542714719]
print (", ".join(Floatlist))

but i am getting an following error :
TypeError: sequence item 0: expected string, float found

but i want to print the list like: 但我想像这样打印列表:

output:14.715258933890,10.215953824,14.8171645397,10.2458542714719

You need to convert the elements to strings. 您需要将元素转换为字符串。

print (", ".join(map(str, Floatlist)))

or 要么

print (", ".join(str(f) for f in Floatlist)))

.join only operates on iterables that yield strings. .join仅对产生字符串的可迭代对象进行操作。 It doesn't convert to strings implicitly for you -- You need to do that yourself: 它不会为您隐式转换为字符串-您需要自己执行此操作:

','.join([str(f) for f in FloatList])

','.join(str(f) for f in FloatList) also works (notice the missing square brackets), but on CPython, it's pretty well known that the version with the list comprehension performs slightly better. ','.join(str(f) for f in FloatList)中也可以(注意缺少方括号中),但在CPython的,这是很众所周知,与列表理解的版本进行略胜一筹

You just make a for statement: 您只需做一个for语句:

for i in Floatlist:
    print(i, ', ', end='')

Hope that helps 希望能有所帮助

PS: This snippet code only works in Python3 PS:此代码段代码仅适用于Python3

Just to print: 只是打印:

print(str(Floatlist).strip('[]'))
#out: 14.71525893389, 10.215953824, 14.8171645397, 10.2458542714719

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

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