简体   繁体   English

需要将字符串列表转换为普通数字列表,字符串是基数10

[英]Need to convert a list of strings to a list of normal numbers, strings are base 10

I have a list of integers looking like this: 我有一个整数列表,看起来像这样:

["1e-04", "1e-06", "1e-08", "1e-10", "1e-12"]

I need to convert this list of strings into a list of normal numbers, but the base 10 gets me stuck. 我需要将这个字符串列表转换为正常数字列表,但是基数10让我陷入困境。 Any suggestions? 有什么建议么? Thanks. 谢谢。

>>> import ast
>>> L = ["1e-04", "1e-06", "1e-08", "1e-10", "1e-12"]
>>> [ast.literal_eval(x) for x in L]
[0.0001, 1e-06, 1e-08, 1e-10, 1e-12]

Or simply.. 或者只是......

>>> [float(x) for x in L]
[0.0001, 1e-06, 1e-08, 1e-10, 1e-12]

I'd use map and float : 我使用mapfloat

>>> values_as_strings = ["1e-04", "1e-06", "1e-08", "1e-10", "1e-12"
>>> values_as_floats = map(float, values_as_strings)
>>> print values_as_floats
[0.0001, 1e-06, 1e-08, 1e-10, 1e-12]

See map . 地图

a=[eval(i) for i in a] # here a is the list a = [eval(i)for i in a]#这里a是列表

this list comprehension will solve it 这个列表理解会解决它

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

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