简体   繁体   中英

Error casting to float, then int in python

Strange error happening.

I know of the issue with trying to cast strings with decimals directly into ints:

int(float('0.0'))

works, while

int('0.0')

does not. However, I'm still getting an error that I can't seem to figure out:

field = line.strip().split()
data[k,:] = [int(float(k)) for k in field[1:]]

ValueError: invalid literal for long() with base 10: '0.0'

Any ideas what could be happening here? The script seems to be thinking it's a cast to long instead of float . Any way to convince it otherwise?

Thanks in advance!

EDIT: the data line is of the form:

'c1c9r2r8\t0.0\t3.4\t2.1\t9.0\n'

It appears that what is happening is that the list comprehension is polluting your namespace.

eg.

k = 0
[k for k in range(10)] 

After executing the above code in python 2.x the value of k will be 9 (the last value that was produced by range(10) ).

I'll simplify your code to show you what is happening.

>>> l = [None, None, None]
>>> k = 0
>>> l[k] = [k for k in range(3)]
>>> print k, l
2 [None, None, [0, 1, 2]]

You see that l[k] evaluated to l[2] rather than l[0] . To avoid this namespace pollution either do not use the same variable names in a list comprehension as you do in the outer code, or use python 3.x where inner variables of list comprehensions no longer escape to the outer code.

For python 2.x your code should be modified to be something like:

data[k,:] = [int(float(_k)) for _k in field[1:]]
>>> line = 'c1c9r2r8\t0.0\t3.4\t2.1\t9.0\n'
>>> field = line.strip().split()
>>> field
['c1c9r2r8', '0.0', '3.4', '2.1', '9.0']
>>> [int(x) for x in map(float, field[1:])]
[0, 3, 2, 9]

Your error is coming from the left-hand side of the assignment data[k, :] = ... . Here you're trying to index a NumPy array ( data ) with a string ( k ). NumPy tries to do an implicit conversion of that string to a usable integer index, and fails. For example:

>>> import numpy as np
>>> data = np.arange(12).reshape(3, 4)
>>> data['3.4', :] = 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for long() with base 10: '3.4'

Use an integer instead of a string here, and the problem should go away.

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