List of strings to list of floats ?

skip at pobox.com skip at pobox.com
Sun Oct 16 00:19:17 EDT 2005


    Madhusudan> Is it possible to convert a very long list of strings to a
    Madhusudan> list of floats in a single statement ?

    Madhusudan> I have tried float(x) and float(x[:]) but neither work. I
    Madhusudan> guess I would have to write a loop if there isn't a way.

Try:

    >>> los = ["123.0", "2", "1e-6"]
    >>> map(float, los)
    [123.0, 2.0, 9.9999999999999995e-07]
    >>> [float(s) for s in los]
    [123.0, 2.0, 9.9999999999999995e-07]

Skip



More information about the Python-list mailing list