Let's say I have list
of lists
in matrix
variable:
matrix = [['first', '1,1', 'last'], ['strng_1', '12231,71', 'st_2']]
As you can see, all nested lists are having float data written as string. I would like to convert them to float
datatype.
I need to create a dictionary and make this conversion simultaneously.
For that reason, I've tried to make it using dictionary comprehension
. So, these operations as one-liner might look like this:
dict_comp = {r[0]: r.insert(1, float(r[1].replace(',', '.'))).pop(2) for r in matrix if r}
But it doesn't work as expected. And now, after my previous question I know why exactly. Finally, I would like to ask about how to generate a dictionary with simultaneous converting certain strings to floats?
UPDATE
Expecting output:
{'first': ['first', 1.1, 'last'], 'strng_1': ['strng_1', 12231.71, 'st_2']}