Home > OS >  Convert Tuple to Float In Python
Convert Tuple to Float In Python

Time:12-24

I have this tuple

t = (['?'], ['?'], [3], [48.0489692022056], [9.32161088395051])

And want to convert the fourth and fifth index to a float number without importing any modules (except math or geodist)

I need that for geodist

if geodist((x, y), (i[3], i[4])) ...:

Because it only accepts tuples!

Error: TypeError: must be real number, not list

CodePudding user response:

You can loop through your tuple and use builtin isinstance() function of python for this:

t = (['?'], ['?'], ['?'], [48.0489692022056], [9.32161088395051])
tuple([i[0] for i in t[3:] if isinstance(i[0], float)])

Output:

(48.0489692022056, 9.32161088395051)

CodePudding user response:

new_t = (float(t[3]), float(t[4]))

  • Related