0

I have a list empty2 which when prints gives the values:

[]
['0', '0', '0', '16', '25', '31', '36', '45', '46', '42']
['0', '0', '0', '0', '0', '0', '0', '4', '11', '23']
['88', '84', '84', '74', '66', '58', '54', '44', '36', '26']
['14', '15', '15', '8', '9', '5', '6', '6', '7', '7']

The function im calling when trying to call items within this list (the individual lists) wants the values in int format.

But I can't seem to find a way to convert each list to int, without destroying the list structure within the lists, which needs to be kept. Any help would great.

saph_top
  • 677
  • 1
  • 6
  • 23
  • This seems like a duplicate of [this question](http://stackoverflow.com/questions/3371269/call-int-function-on-every-list-element-in-python). – MPlanchard Dec 02 '15 at 20:45
  • 1
    `[int(x) for x in ]` – mpez0 Dec 02 '15 at 20:45
  • 1
    This question might not be a duplicate, since it appears the OP is asking about a list of lists of string integers, in which case the answer would be `[[int(x) for x in sublist] for sublist in empty2]` – JCOC611 Dec 02 '15 at 20:46
  • @JCOC611 right I think this it it, my code can get through without an error, but if i set empty3 = [[int(x) for x in sublist] for sublist in empty2] and print empty 3, this list is empty,hmm EDIT: sorry talking rubbish didn;t put empty3 in the right place – saph_top Dec 02 '15 at 20:54

1 Answers1

1
x=[[],
['0', '0', '0', '16', '25', '31', '36', '45', '46', '42'],
['0', '0', '0', '0', '0', '0', '0', '4', '11', '23'],
['88', '84', '84', '74', '66', '58', '54', '44', '36', '26'],
['14', '15', '15', '8', '9', '5', '6', '6', '7', '7']]
print [map(int,i) for i in x ]

This should do it for you.

Output:[[], [0, 0, 0, 16, 25, 31, 36, 45, 46, 42], [0, 0, 0, 0, 0, 0, 0, 4, 11, 23], [88, 84, 84, 74, 66, 58, 54, 44, 36, 26], [14, 15, 15, 8, 9, 5, 6, 6, 7, 7]]

vks
  • 67,027
  • 10
  • 91
  • 124
  • this throws the error TypeError: unsupported operand type(s) for -: 'list' and 'int' because, sorry quoted the text wrong , the lists are inside another list...i think? – saph_top Dec 02 '15 at 20:49
  • @user3244770 its working for me.Can you post the exact way `empty2` was created – vks Dec 02 '15 at 20:50