2

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'
4
  • Because '0.0' has a point. It can't transform integer. Commented Nov 21, 2014 at 3:46
  • You miss the point. Starting python up interactively and entering int(float('0.0')) returns 0. Commented Nov 21, 2014 at 3:47
  • Try int(0.0) (Exclude the single quotes) Commented Nov 21, 2014 at 3:54
  • What is k in data[k,:]? Are you intending to index data with the last value of field? If you are then this code is very unclear, and will not work in python 3.x Commented Nov 21, 2014 at 12:59

3 Answers 3

4

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:]]
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, you're totally right. Didn't catch that. I thought I was going crazy!
2
>>> 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]

2 Comments

It works! Thank you kindly, @Puffin-GDI! If I may ask, any idea why the above would not work and this does?
I think it's work, too. Maybe you checking your code by question. data[k,:] or other errors. :)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.