简体   繁体   English

对如何仅从列表中打印数字并将其转换为浮点数感到困惑

[英]Confused on how to print numbers only from list and convert them into float

my code:-我的代码:-

mean_distances = [td.text.strip() for td in rows[3].find_all('td') ]

Output:- Output:-

mean_distances: ['Mean distance from the Sun', 'km   AU', '57,909,175  
 0.38709893', '108,208,930   0.72333199', '149,597,890   1.00000011', '227,936,640   1.52366231', '778,412,010   5.20336301', '1,426,725,400  
 9.53707032', '2,870,972,200   19.19126393', '4,498,252,900   30.06896348']

I want to extract only the numbers in km (ignore the second number expressing the distance in AU) and covert the str data to float value.我只想提取 km 中的数字(忽略表示 AU 中距离的第二个数字)并将 str 数据转换为浮点值。 How can I do it?我该怎么做?

# loop over the items in mean_distances
# skip the first two rows which are the caption and column headers
for item in mean_distances[2:]:

    # split this item into km and au
    km, au = item.split()

    # remove commas from km
    km = km.replace(",", "")

    # convert km to float
    km = float(km)
float_value = [float(item.split()[1]) for item in mean_distances[2:]]

If you know that the items like Mean distance from the Sun and km AU will always take up exactly the first two items, you can use the str.split() method to cut off everything after the first space and the float() method to convert it to a float:如果您知道诸如Mean distance from the Sunkm AU之类的项目将始终占据前两项,您可以使用str.split()方法将第一个空格之后的所有内容和float()方法截断将其转换为浮点数:

temp = []
for distance in mean_distances[2:]:
    temp.append(float(distance.split(" ")[0].replace(",", "")))
mean_distances = temp

This gives an output of [57909175.0, 108208930.0, 149597890.0, 227936640.0, 778412010.0, 1426725400.0, 2870972200.0, 4498252900.0] .这给出了[57909175.0, 108208930.0, 149597890.0, 227936640.0, 778412010.0, 1426725400.0, 2870972200.0, 4498252900.0]

Try this:尝试这个:

m=[]
for i in range(2,len(l)):
    m.append(l[i].split()[0])
    res=[float(i.replace(',', '')) for i in m]
  
>>> print(res)

[57909175.0, 108208930.0, 149597890.0, 227936640.0, 778412010.0, 1426725400.0, 2870972200.0, 4498252900.0]

If you want to keep ',' as thousands separators, to be more readable, see here for some solutions:如果您想保留 ',' 作为千位分隔符,以提高可读性,请参阅此处了解一些解决方案:

How to print number with commas as thousands separators? 如何用逗号打印数字作为千位分隔符?

暂无
暂无

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

相关问题 Python-如何仅将混合列表中的数字转换为浮点数? - Python - How to convert only numbers in a mixed list into float? 如何将字符串和数字列表转换为在 python 中浮动? - How to convert a list of strings and numbers to float in python? 如何仅在一行中获取 n 个数字并将它们放入列表中,并根据列表打印 python 中的字符? - How to get the n numbers in only one line and put them into a list and according to the list, print characters in python? 如何将 sympy.core.numbers.Float 类型转换为原生 python float 类型,或直接转换为 plot 它们 - How to convert sympy.core.numbers.Float types to just native python float type, or directly plot them 如何比较列表中的项目以查找奇数并打印? - How to compare items in list to find odd numbers and print them? 如何将大量用逗号分隔的数字转换为float - How to convert a big list of comma-separated numbers into float 在Python中,如何将浮点数列表转换为具有特定格式的字符串? - in Python, how to convert list of float numbers to string with certain format? 如何将字符串列表转换为浮点数数组? - How can I convert a list of strings to an array of float numbers? 数据框列的数字很多是 str 格式,很多是浮点数,我如何将它们全部转换为浮点数 - Dataframe columns have numbers many of them in str format and many in float, how do I convert all of them to float 如何转换为浮动列表? - How to convert a list in a float?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM