简体   繁体   English

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

[英]How do I convert a list of floats into a list of percentages?

I have this list:我有这个清单:

list = [
  0.6490486257928119,
  0.2996742671009772,
  0.589242053789731
]

I would like to convert it into a list of percentages with one decimal, like this:我想将其转换为带一位小数的百分比列表,如下所示:

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

I don't know how to proceed. 

You could use list comprehension.您可以使用列表理解。 Be aware however, that the result you would like changes the type of the items from float to str because of the percentage symbol that you would like to include.但是请注意,由于您想要包含百分比符号,您想要的结果会将项目的typefloat更改为str

As a side note: you shouldn't use list as your variable name because that way, you are overwriting Pythons list method.附带说明:您不应该使用list作为变量名,因为那样的话,您将覆盖 Python 的list方法。

lst = [0.6490486257928119, 0.2996742671009772, 0.589242053789731]

new_lst = [f'{i*100:.1f}%' for i in lst]

print(new_lst)

You could do that with list comprehension.您可以通过列表理解来做到这一点。 For example:例如:

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]

if u want to have %, u can convert to string如果你想有 %,你可以转换成字符串

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

new_list = ["64.9%", "29.9%", "58.9%"]
print("{0:.1f}%".format(0.6490486257928119*100))

Part 1:第1部分:

"{0:.1f}%"

This is the "done format section" the percentage symbol, it doesn't needs explanation... {in here} the formatted text is placed here '0' (zero) is the index of the integer, what we want to format.这是“完成格式部分”的百分比符号,不需要解释... {in here}格式化的文本放在这里'0'(零)是integer的索引,我们要格式化。 In here is the 0.649048...x100 .这里是0.649048...x100 The ".1" specifies the location of the decimal point and the " f " is for the float “.1”指定小数点的位置,“ f ”用于浮点数

Part 2:第2部分:

.format(0.6490486257928119*100)

And in the end of the .format() function where we give the integer, or double, or float.format() function 的末尾,我们给出 integer,或 double,或 float

REALLY REALLY SORRY FOR MY ENGLISH!!!真的真的对不起我的英语!!!

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

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