Home > Enterprise >  How to change multiple values in a list of lists from str to float
How to change multiple values in a list of lists from str to float

Time:07-28

I have the following list:

[[
     ['3.31497114423,50.803721015'],
     ['7.09205325687,50.803721015'],
     ['7.09205325687,53.5104033474'],
     ['3.31497114423,53.5104033474'], 
     ['3.31497114423,50.803721015']
         ]]

How can I convert the list so that each of the value is a floating point?

Wanted result:

[[
     [3.31497114423,50.803721015],
     [7.09205325687,50.803721015],
     [7.09205325687,53.5104033474],
     [3.31497114423,53.5104033474],
     [3.31497114423,50.803721015]
         ]]

CodePudding user response:

You can use list comprehension and split string base ',' then use map(float, list) for converting each str in list to float.

res = [[list(map(float,l[0].split(','))) for l in lst] for lst in lst_lst]

print(res)

[[
    [3.31497114423, 50.803721015], 
    [7.09205325687, 50.803721015], 
    [7.09205325687, 53.5104033474], 
    [3.31497114423, 53.5104033474], 
    [3.31497114423, 50.803721015]
]]

Explanation:

>>> '3.31497114423,50.803721015'.split(',')
['3.31497114423', '50.803721015']

>>> list(map(float, '3.31497114423,50.803721015'.split(',')))
[3.31497114423, 50.803721015]

CodePudding user response:

You can just eval the string:

a=[[
     ['3.31497114423,50.803721015'],
     ['7.09205325687,50.803721015'],
     ['7.09205325687,53.5104033474'],
     ['3.31497114423,53.5104033474'], 
     ['3.31497114423,50.803721015']
         ]]
a=[[list(eval(i[0])) for i in a[0]]]
print(a)

Output:

[[[3.31497114423, 50.803721015], [7.09205325687, 50.803721015], [7.09205325687, 53.5104033474], [3.31497114423, 53.5104033474], [3.31497114423, 50.803721015]]]

CodePudding user response:

x = [[['3.31497114423,50.803721015'],
['7.09205325687,50.803721015'],
['7.09205325687,53.5104033474'],
['3.31497114423,53.5104033474'],
['3.31497114423,50.803721015']]]
    
y = [[]]
for item in x[0]:
    t = []
    z = item[0].split(',')
    for f in z:
        t.append(float(f))
    y[0].append(t)
        
print(y)

CodePudding user response:

You could also use this monstrosity which uses only one list comprehension:

[list(map(float,p[0][i][0].split(","))) for i in range(len(p[0]))]

, where p is your original array. The solution of Ash Nazg is more elegant though. But be careful with using eval() on untrusted data.

CodePudding user response:

I'd suggest breaking the task into two simpler ones. First you want some code that turns a string containing comma-separated floats into a list of floats, something like:

def parse_csv_floats(s):
    return list(map(float, s.split(",")))

parse_csv_floats("3.1,50.8")  # => [3.1, 50.8]

and then you want to call this code for each element of your egregiously nested structure:

deep_csv_floats = [[
     ['3.31497114423,50.803721015'],
     ['7.09205325687,50.803721015'],
     ['7.09205325687,53.5104033474'],
     ['3.31497114423,53.5104033474'], 
     ['3.31497114423,50.803721015']
]]
deep_floats = [
    [parse_csv_floats(csv) for [csv] in inner]
    for inner in deep_csv_floats
]

which stores your expected output in deep_floats. Note that the for [csv] in inner will ensure that the CSVs are always exactly one string in a list. If you want to parse values that are more nested, you will want another nested list comprehension.

  • Related