简体   繁体   中英

How do you change a list of integers and strings into a list of integers and floats in python?

I have a long list with a bunch of lists:

[0, ['0.2000', '0.2000', '3.0000', '0.5000']]

How do I make the '...' floats and keep the integers (0) integers?

I've tried numpy, but it doesn't work for me numpy.array(list, numpy.float)

I don't really mind if the integers are also floats.

I'd do something like this:

newlist = [[element[0], [float(e) for e in element[1]] for element in oldlist]

element[0] is the integer that's left as-is, element[1] is the list of strings that are converted to floats in the inner list comprehension.

I need to see more data to know for sure if this fits, but this might come close:

l1 = [0, ['0.2000', '0.2000', '3.0000', '0.5000']]
l2 = [x if type(x) is int else map(float, x) for x in l1]

Again, need to see more about your actual data to know for certain if this works. The above returns:

[0, [0.20000000000000001, 0.20000000000000001, 3.0, 0.5]]

Here's how you could do it with numpy:

In [96]: l1=[0, ['0.2000', '0.2000', '3.0000', '0.5000']]

In [97]: l2=np.array(tuple(l1),dtype=[('f1','int'),('f2','float',4)])

In [98]: l2
Out[98]: 
array((0, [0.20000000000000001, 0.20000000000000001, 3.0, 0.5]), 
      dtype=[('f1', '<i4'), ('f2', '<f8', 4)])

If your actual list looks more like this:

In [99]: l3=[0, ['0.2000', '0.2000', '3.0000', '0.5000'], 1, ['.1','.2','.3','0.4']]

then you could use the grouper idiom zip(*[iter(l3)]*2)) to group every 2 elements into a tuple:

In [104]: zip(*[iter(l3)]*2)
Out[104]: [(0, ['0.2000', '0.2000', '3.0000', '0.5000']), (1, ['.1', '.2', '.3', '0.4'])]

and pass this on to np.array :

In [105]: l4=np.array(zip(*[iter(l3)]*2),dtype=[('f1','int'),('f2','float',4)])

In [106]: l4
Out[106]: 
array([(0, [0.20000000000000001, 0.20000000000000001, 3.0, 0.5]),
       (1, [0.10000000000000001, 0.20000000000000001, 0.29999999999999999, 0.40000000000000002])], 
      dtype=[('f1', '<i4'), ('f2', '<f8', 4)])

simply

li = [0, ['0.2000', '0.2000', '3.0000', '0.5000']]
print li

li[1] = map(float,li[1])
print li

EDIT 1

If xyze is:

xyze = [[0, ['0.2000', '0.2000', '3.000' , '0.5000']],
        [9, ['0.1450', '0.8880', '3.000' , '0.4780']],
        [4, ['5.0025', '7.2000', '12.00' , '6.5013']]]

do

print '\n'.join(map(str,xyze))
print

for el in xyze:
    el[1] = map(float,el[1])

print '\n'.join(map(str,xyze))

result

[0, ['0.2000', '0.2000', '3.000', '0.5000']]
[9, ['0.1450', '0.8880', '3.000', '0.4780']]
[4, ['5.0025', '7.2000', '12.00', '6.5013']]

[0, [0.2, 0.2, 3.0, 0.5]]
[9, [0.145, 0.888, 3.0, 0.478]]
[4, [5.0025, 7.2, 12.0, 6.5013]]

If you wish to flatten the nested lists as well...

import types

def process(l):
    for item in l:
        if isinstance(item, types.ListType):
            for item in process(item):
                yield float(item)
        else:
            yield float(item)

>>> l = [0, 1, 2, [3, 4, '5.000'], ['6.01', 7, [8, 9]]]
>>> list( process(l) )
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.01, 7.0, 8.0, 9.0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM