0

I have an List like down below

[['-6.167665', '106.904251'], ['-6.167665', '106.904251']]

How to make it like this?

[[-6.167665, 106.904251], [-6.167665, 106.904251]]

Any suggestions how to do it?

4
  • Looks like you want to covert strings to floats? Commented Jan 17, 2023 at 1:29
  • yes it seems i have a float dataframe that converted to strings if i make them into list, and i need them in list of float Commented Jan 17, 2023 at 1:31
  • 1
    If you have a dataframe, why don't you convert it to float in it rather than the list? Commented Jan 17, 2023 at 1:35
  • 1
    From a DataFrame: df.astype(float).to_numpy().tolist() Commented Jan 17, 2023 at 1:38

5 Answers 5

1

You want to use float.

original_list = [['-6.167665', '106.904251'], ['-6.167665', '106.904251']]
print(original_list)

new_list = [[float(i) for i in inner] for inner in original_list]
print(new_list)
Sign up to request clarification or add additional context in comments.

Comments

1

For big arrays numpy would do the trick :

import numpy as np
l1 = [['-6.167665', '106.904251'], ['-6.167665', '106.904251']]
l2 = np.array(l1, dtype=np.float32)

output:

[[-6.167665, 106.904251], [-6.167665, 106.904251]]

Comments

0
old = [['-6.167665', '106.904251'], ['-6.167665', '106.904251']]
new = [list(map(lambda string: float(string), item)) for item in old]

Comments

0

Other answers already have suggested the method of doing it, which is convert to float. The most efficient way to do it is map:

values = [['-6.167665', '106.904251'], ['-6.167665', '106.904251']]

float_values = [list(map(float, vals)) for vals in values]

print(float_values)

Gives you:

[[-6.167665, 106.904251], [-6.167665, 106.904251]]

Comments

-1

You have to convert it to floats

old = [['-6.167665', '106.904251'], ['-6.167665', '106.904251']]

new = [list(map(float, i)) for i in old]

new
[[-6.167665, 106.904251], [-6.167665, 106.904251]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.