views:

155

answers:

1

I'm reading some numbers from a data source that represent xy coordinates that I'll be using for a TSP-esque problem. I'm new to python, so I'm trying to make the most of lists. After reading and parsing through the data, I'm left with a list of string lists that looks like this:

[['565.0', '575.0'], ['1215.0', '245.0'], ...yougetthepoint... ['1740.0', '245.0']]

I would rather be dealing with integer points. How can I transform these lists containing strings to lists containing ints? They don't seem to be casting nicely, as I get this error:

ValueError: invalid literal for int() with base 10: '565.0'

The decimal seems to be causing issues.

+6  A: 
x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
x = [[int(float(j)) for j in i] for i in x]
Max Shawabkeh
ValueError: invalid literal for float(): . <--hmm..
Chris
What broken generator produced just a period?
Ignacio Vazquez-Abrams
None of them are just periods. That's what's curious. It's running from inside of textmate.
Chris
Perhaps you are running this on a list of strings rather than a list of lists of strings, or perhaps one of the list's element is a string and the rest are lists? In that case it will try to convert each character separately, which would bring up a period as an element.
Max Shawabkeh
Works fine from interactive mode. Que slique!
jathanism
Yeah, I'm not sure what's going on but I just tested this in a separate case and this is the answer. Thanks Max S
Chris