Convert '165.0' to int

SigmundV sigmundv at gmail.com
Sun Jul 24 14:27:44 EDT 2011


On Jul 21, 10:31 am, "Frank Millman" <fr... at chagford.com> wrote:
> Is there a short cut, or must I do this every time (I have lots of them!) ?
> I know I can write a function to do this, but is there anything built-in?

I'd say that we have established that there is no shortcut, no built-
in for this. You write you own function:

string_to_int = lambda s: int(float(s))

Then you apply it to your list of strings:

list_of_integers = map(string_to_int, list_of_strings)

Of course, this will be horribly slow if you have thousands of
strings. In such a case you should use an iterator (assuming you use
python 2.7):

import itertools as it
iterator = it.imap(string_to_int, list_of_strings)


Regards,

Sigmund



More information about the Python-list mailing list