2 回答
TA贡献1874条经验 获得超12个赞
只需使用列表理解
values_list = ['6.26%', '5.94%', '7.47%', '6.90%', '5.99%', '7.94%',
'8.75%', '9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%',
'2.97%', '6.38%', '4.93%', '3.96%', '4.62%', '3.73%', '5.15%',
'0.86%', '2.68%', '4.01%', '4.89%', '5.84%', '5.23%', '5.57%',
'5.53%', '2.39%', '1.00%', '2.44%', '4.65%', '3.66%', '4.60%',
'4.54%', '2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%',
'3.55%', '3.48%', '1.13%', '3.45%']
float_list = [float(value.strip('%')) for value in values_list]
这导致:
[6.26, 5.94, 7.47, 6.9, 5.99, 7.94, 8.75, 9.01, 10.07, 5.51, 4.87,
5.88, 4.26, 2.97, 6.38, 4.93, 3.96, 4.62, 3.73, 5.15, 0.86,
2.68, 4.01, 4.89, 5.84, 5.23, 5.57, 5.53, 2.39, 1.0, 2.44, 4.65,
3.66, 4.6, 4.54, 2.3, -1.51, 2.36, 3.13, 3.12, 1.28, 3.55,
3.48, 1.13, 3.45]
TA贡献1864条经验 获得超6个赞
一种简单的方法(但效率更低)。使用 for 循环从字符串中删除每个 %。然后您可以将它们转换为浮点数。例如:
l = ['6.26%', '5.94%', '7.47%', '6.90%', '5.99%', '7.94%', '8.75%',
'9.01%', '10.07%', '5.51%', '4.87%', '5.88%', '4.26%', '2.97%',
'6.38%', '4.93%', '3.96%', '4.62%', '3.73%', '5.15%', '0.86%',
'2.68%', '4.01%', '4.89%', '5.84%', '5.23%', '5.57%', '5.53%',
'2.39%', '1.00%', '2.44%', '4.65%', '3.66%', '4.60%', '4.54%',
'2.30%', '-1.51%', '2.36%', '3.13%', '3.12%', '1.28%', '3.55%',
'3.48%', '1.13%', '3.45%']
new = []
for item in l:
new.append(float(l[:-1]))
l = new
新现在等于你的通缉名单
编辑: int() 函数实际上应该是 float()。这将导致字符串不会丢失其十进制值。
添加回答
举报