-3

I have a list of string numbers as follow:

my_list = ["6.84", "6", "3.24"]

and I expect to find a simple solution for convert those numbers which are in string format to digit format:

my_list = [6.84, 6, 3.24]

What I have tried:

l = [float(num) if '.' in num else int(num) for num in my_list]

I am wondering if there is a more straight forward solution in python?

ElisaFo
  • 130
  • 13
  • `list(map(float, my_list))`. I realize this isn't equivalent, but having mixed types seems undesirable. Please [offer context for why you need this](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – ggorlen Dec 05 '19 at 21:53
  • @ggorlen that is not equivalent, since it doesn't map integers to ints. – kaya3 Dec 05 '19 at 21:54
  • If all of your floats have `.` in them, then this is fine. If you have any floats written like `1e3` then it won't work. – kaya3 Dec 05 '19 at 21:54
  • @ggorlen your solution converts 6 to 6.0 which is not what I expect. I need it for a printing purpose – ElisaFo Dec 05 '19 at 21:55
  • Good point, I will try to fix it then. thanks – ElisaFo Dec 05 '19 at 21:58
  • @ggorlen Why would you have to check the type when using them? They're all numbers, arithmetic operations in Python work just fine on mixed ints and floats. One advantage of using `int` for the integers is that Python supports arbitrarily large ints, but floating point precision is limited. – kaya3 Dec 05 '19 at 21:58
  • But what use-case *would* require type-checking with `isinstance`? What can you do in Python with a float that you can't also do with an int? Even floating-point specific functions like `math.isnan` work fine with int arguments. – kaya3 Dec 05 '19 at 22:03
  • 2
    If your problem is printing, just use the right printing format. Have a look at the `'g'` presentation type in https://docs.python.org/3/library/string.html#format-specification-mini-language, it will remove the unnecessary trailing zeros and dot. – Thierry Lathuille Dec 05 '19 at 22:05
  • @ggorlen That doesn't follow from what I said, and I've already given an example of how using `int` for integers can be preferred. The fact that you can use ints instead of floats polymorphically without `isinstance` type-checking doesn't imply that there is no point to using ints. – kaya3 Dec 05 '19 at 22:05
  • I can give a concrete use-case from my own experience - an educational application which asks students to write example data of a particular type, and then checks whether they wrote the correct type. Just because that probably isn't what the OP is doing doesn't mean there is anything wrong with the question or that it shouldn't be answered. – kaya3 Dec 05 '19 at 22:09
  • I already blew my vote but likely a dupe of [Formatting floats in Python without trailing zeroes](https://stackoverflow.com/questions/2440692/formatting-floats-in-python-without-trailing-zeros). – ggorlen Dec 05 '19 at 22:31

1 Answers1

2

Your solution is absolutely fine; the only way it can be directly simplified is to factor out the (num) function call:

l = [(float if '.' in num else int)(num) for num in my_list]

But it's a matter of opinion whether that's better.

A separate issue is that '.' in num doesn't test for all kinds of string representations of a floating point number, e.g. 1e3 is the floating point number 1000.0. If you want to parse every numeric string as the type it would be in Python, you can use the ast.literal_eval function:

import ast

l = [ast.literal_eval(num) for num in my_list]
kaya3
  • 47,440
  • 4
  • 68
  • 97