views:

3970

answers:

8

Hello there,

I have a tuple of tuples from MySQL query like this:

T1 = (('13', '17', '18', '21', '32'),
      ('07', '11', '13', '14', '28'),
      ('01', '05', '06', '08', '15', '16'))

I'd like to convert all the string elements into integers and put it back nicely to list of lists this time:

T2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

I tried to achieve it with "eval" but didn't get any decent result yet.

+14  A: 

int() is the Python function to convert a string into an integer value.

If you know the structure of your list (that it simply contains lists, only one level), you could do this:

T2 = [map(int, x) for x in T1]
unwind
Thank you! That's exactly what I needed.
Great! Actually, the example given also works for "naked" numbers in the input, but will wrap them in one-element lists.
unwind
@unwind Nice answer
aatifh
doesn't work in py3k
SilentGhost
+1  A: 

use int() to convert string to int

Samuel
+4  A: 

You can do this with a list comprehension:

T2 = [[int(column) for column in row] for row in T1]

The inner list comprehension ([int(column) for column in row]) builds a list of ints from a sequence of int-able objects, like decimal strings, in row. The outer list comprehension ([... for row in T1])) builds a list of the results of the inner list comprehension applied to each item in T1.

The code snippet will fail if any of the rows contain objects that can't be converted by int. You'll need a smarter function if you want to process rows containing non-decimal strings.

If you know the structure of the rows, you can replace the inner list comprehension with a call to a function of the row. Eg.

T2 = [parse_a_row_of_T1(row) for row in T1]
Will Harris
A: 

If it's only a tuple of tuples, something like rows=[map(int, row) for row in rows] will do the trick. (There's a list comprehension and a call to map(f, lst), which is equal to [f(a) for a in lst], in there.)

Eval is not what you want to do, in case there's something like __import__("os").unlink("importantsystemfile") in your database for some reason. Always validate your input (if with nothing else, the exception int() will raise if you have bad input).

AKX
A: 

Using list comprehensions:

t2 = [map(int, list(l)) for l in t1]
Jon
+1  A: 

I would agree with everyones answers so far but the problem is is that if you do not have all integers they will crash.

If you wanted to exclude non-integers then

T1 = (('13', '17', '18', '21', '32'),
      ('07', '11', '13', '14', '28'),
      ('01', '05', '06', '08', '15', '16'))
new_list = list(list(int(a) for a in b) for b in T1 if a.isdigit())

This yields only actual digits. The reason I don't use direct list comprehensions is because list comprehension leaks their internal variables.

Christian Witts
A: 

I would rather prefer using only comprehension lists:

[[int(y) for y in x] for x in T1]
odwl
A: 

L1 = [ '1', '3', '2', '6', '7', '8', '9', '10', '11'] L2 = [ 1, 3, 2, 6, 7, 8, 9, 10, 11] Using Critian Witts's code I got L2 from L1 but now I can't sort L2 by L2.sort() I finally want L3 =[1,2,3,4,5,6,7,8,9,10,11] please help.

Shamim
post a new question, and L2.sort() should work (it sorts in place).
N 1.1