python将浮点数列表转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23418600/
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
python convert a list of float to string
提问by Niebieski
I have a list of floats in Python and when I convert it into a string, I get the follwoing
我在 Python 中有一个浮点数列表,当我将其转换为字符串时,我得到以下信息
[1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
These floats have 2 digits after the decimal point when I created them (I believe so),
当我创建它们时,这些浮点数在小数点后有 2 位数字(我相信是这样),
Then I used
然后我用
str(mylist)
How do I get a string with 2 digits after the decimal point?
如何获得小数点后两位数的字符串?
======================
======================
Let me be more specific:
让我更具体一点:
I want to get something like "[1883.95, 1878.33, 1869.43, 1863.40]"
我想得到类似“[1883.95, 1878.33, 1869.43, 1863.40]”的东西
I need to some string operation afterwards. For example +="!\t!"
之后我需要一些字符串操作。例如 +="!\t!"
I want the end result to be a string and I want to keep the separators.
我希望最终结果是一个字符串,我想保留分隔符。
Of course we all know how to do it in a loop, but hopefully someone can come up a very smart&elegant way to it.
当然,我们都知道如何循环执行,但希望有人能想出一种非常聪明和优雅的方法。
inspired by @senshin the following code works for example, but I think there is a better way
例如,受@senshin 启发,以下代码有效,但我认为有更好的方法
msg = "["
for x in mylist:
msg += '{:.2f}'.format(x)+','
msg = msg[0:len(msg)-1]
msg+="]"
print msg
Thanks!
谢谢!
采纳答案by senshin
Use string formatting to get the desired number of decimal places.
使用字符串格式获得所需的小数位数。
>>> nums = [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
>>> ['{:.2f}'.format(x) for x in nums]
['1883.95', '1878.33', '1869.43', '1863.40']
The format string {:.2f}
means "print a fixed-point number (f
) with two places after the decimal point (.2
)". str.format
will automatically round the number correctly (assuming you entered the numbers with two decimal places in the first place, in which case the floating-point error won't be enough to mess with the rounding).
格式字符串的{:.2f}
意思是“打印一个f
小数点后两位()的定点数(.2
)”。str.format
将自动正确舍入数字(假设您首先输入的数字带有两位小数,在这种情况下,浮点错误不足以影响舍入)。
回答by Torkel Bj?rnson-Langen
map(lambda n: '%.2f'%n, [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001])
map()
invokes the callable passed in the first argument for each element in the list/iterable passed as the second argument.
map()
为作为第二个参数传递的列表/迭代中的每个元素调用传入第一个参数的可调用对象。
回答by Karl I.
Get rid of the ' marks:
去掉 ' 标记:
>>> nums = [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
>>> '[{:s}]'.format(', '.join(['{:.2f}'.format(x) for x in nums]))
'[1883.95, 1878.33, 1869.43, 1863.40]'
- ['{:.2f}'.format(x) for x in nums]makes a list of strings, as in the accepted answer.
- ', '.join([list])returns one string with ', ' inserted between the list elements.
- '[{:s}]'.format(joined_string)adds the brackets.
- ['{:.2f}'.format(x) for x in nums] 生成一个字符串列表,如已接受的答案。
- ', '.join([list])返回一个字符串,其中在列表元素之间插入了 ', '。
- '[{:s}]'.format(joined_string)添加括号。