如何将浮点数列表转换为百分比列表?

问题描述 投票:1回答:3

我有此列表:

list = [
  0.6490486257928119,
  0.2996742671009772,
  0.589242053789731
]

我想将其转换为带有一个小数的百分比列表,如下所示:

list = [
  64.9%,
  29.9%,
  58.9%
]

I don't know how to proceed. 
python list percentage
3个回答
1
投票
您可以使用列表理解。但是请注意,由于要包括的百分比符号,因此您希望将结果的typefloat更改为str

作为旁注:您不应使用list作为变量名,因为那样的话,您将覆盖Pythons list方法。

lst = [0.6490486257928119, 0.2996742671009772, 0.589242053789731] new_lst = [f'{i*100:.1f}%' for i in lst] print(new_lst)


0
投票
您可以通过列表理解来做到这一点。例如:

list = [0.6490486257928119, 0.2996742671009772, 0.589242053789731] new_list = [ round(x*100,1) for x in list] #each value in list multiplied by 100 and round up to 1 floating number new_list = [64.9, 29.9, 58.9]

如果您想拥有%,可以转换为字符串

new_list = [ str(round(x*100,1))+"%" for x in list] new_list = ["64.9%", "29.9%", "58.9%"]


0
投票
print("{0:.1f}%".format(0.6490486257928119*100))
© www.soinside.com 2019 - 2024. All rights reserved.