Join float list into space-separated string in Python

后端 未结 2 1913
-上瘾入骨i
-上瘾入骨i 2021-02-03 19:27

I have a list of floats in python:

a = [1.2, 2.9, 7.4]

I want to join them to produce a space-separated string - ie.:

1.2 2.9 7         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 19:54

    You need to convert each entry of the list to a string, not the whole list at once:

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

    If you want more control over the conversion to string (e.g. control how many digits to print), you can use

    print "".join(format(x, "10.3f") for x in a)
    

    See the documentation of the syntax of format specifiers.

提交回复
热议问题