I have a list populated like so with the numeric values subject to being updated as well as the amount of entries in the list. It could have 1 entry, it could have 10.
# list is called m1[i]
'probability': '0.00374'
'probability': '0'
'probability': '0.0978e-18'
When I print(m1) I get 'probability': '0.00374', 'probability': '0', 'probability': '0.0978e-18'
. I want to be able to take just the numbers [0.00374, 0, 0.0978e-18]
. So a combination of Ints, Floats, and Scientific notation strings.
When it was just integers, I used this which worked fine but I am not sure how to change it to include the float or scientific notation.
CP_num = list(map(lambda sub:int(''.join([ele for ele in sub if ele.isnumeric()])), m1))
End result should output as [0.00374, 0, 0.0978e-18]
. Bonus if it can convert scientific notation to a float in the process.
CodePudding user response:
int()
and float()
can be called on a string containing numbers directly. one of these should work:
CP_num = [map(lambda sub: float(sub), m1)]
CP_num = [map(lambda sub: float(num) for num in sub, m1)]
CodePudding user response:
Given this list:
m=['probability: 0.00374',
'probability: 0',
'probability: 0.0978e-18']
You can do:
>>> [float(t[1]) for t in map(lambda s: s.split(':'), m)]
[0.00374, 0.0, 9.78e-20]