Converting List of String to Integer

Andrew Freeman alif016 at gmail.com
Mon Jul 21 19:00:23 EDT 2008


Samir wrote:
> On Jul 21, 6:15 pm, Andrew Freeman <alif... at gmail.com> wrote:
>   
>> Samir wrote:
>>     
>>> On Jul 21, 3:20 pm, Gary Herron <gher... at islandtraining.com> wrote:
>>>       
>>>> Samir wrote:
>>>>         
>>>>> Hi Everyone,
>>>>>           
>>>>> I am relatively new to Python so please forgive me for what seems like
>>>>> a basic question.
>>>>>           
>>>>> Assume that I have a list, a, composed of nested lists with string
>>>>> representations of integers, such that
>>>>>           
>>>>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>>>>>           
>>>>> I would like to convert this to a similar list, b, where the values
>>>>> are represented by integers, such as
>>>>>           
>>>>> b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
>>>>>           
>>>>> I have unsuccessfully tried the following code:
>>>>>           
>>>>> n = []
>>>>> for k in a:
>>>>>     n.append([int(v) for v in k])
>>>>> print n
>>>>>           
>>>>> Does anyone know what I am doing wrong?
>>>>>           
>>>>> Thanks in advance.
>>>>>           
>>>>> Samir
>>>>> --
>>>>> http://mail.python.org/mailman/listinfo/python-list
>>>>>           
>>>> You didn't tell us how it failed for you, so I can't guess what's wrong.
>>>>         
>>>> However, your code works for me:
>>>>         
>>>>  >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>>>>  >>> n = []
>>>>  >>> for k in a:
>>>> ...    n.append([int(v) for v in k])
>>>> ...
>>>>  >>> print n
>>>> [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
>>>>         
>>>> (Although you seem to have confused variables b and n.)
>>>>         
>>>> Gary Herron- Hide quoted text -
>>>>         
>>>> - Show quoted text -
>>>>         
>>> Hi Gary,
>>>       
>>> Thanks for your quick response (and sorry about mixing up b and n).
>>> For some reason, the logic I posted seems to work ok while I'm using
>>> the Python shell, but when used in my code, the program just hangs.
>>> It never outputs the results.  Below is the code in its entirety.  Is
>>> there a problem with my indendentation?
>>>       
>>> a = n = []
>>> t = """
>>> 1 2
>>> 3
>>> 4 5 6
>>> 7 8 9 0
>>> """
>>>       
>>> d = t.split("\n")
>>>       
>>> for x in range(1,len(d)-1):
>>>     a.append(d[x].split(" "))
>>> print a
>>>       
>>> for k in a:
>>>     n.append([int(v) for v in k])
>>>       
>>> print n
>>>       
>>> Thanks again.
>>>       
>>> Samir
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>       
>> I think this will work better, a sub-list comprehension of sorts:
>> n = [[int(i) for i in k] for k in a]
>>
>> here is an ipython interactive session using it:
>> In [1]: a = n = []
>>
>> In [2]: t = """
>>    ...: 1 2
>>    ...: 3
>>    ...: 4 5 6
>>    ...: 7 8 9 0
>>    ...: """
>>
>> In [3]:
>>
>> In [4]: d = t.split("\n")
>>
>> In [5]: for x in range(1,len(d)-1):
>>    ...:     a.append(d[x].split(" "))
>>    ...:    
>>    ...:    
>>
>> In [6]: a
>> Out[6]: [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>>
>> In [7]: n = [[int(i) for i in k] for k in a]
>>
>> In [8]: n
>> Out[8]: [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
>> --
>> Andrew- Hide quoted text -
>>
>> - Show quoted text -
>>     
>
> Andrew,
>
> Thanks for the tip, though the syntax makes my head spin a bit in
> trying to comprehend it.  For my small list, I didn't notice a
> discernible increase in speed, but I may have to try it with a larger
> list size.
>
> Incidentally, I had never heard of iPython but from their web site, it
> looks like an interesting tool.  I'll have to check it out.
>
> Thanks.
>
> Samir
> --
> http://mail.python.org/mailman/listinfo/python-list
>   
If it helps look at this:

n = [[int(i) for i in k] for k in a]

like this:

n = []
for k in a:
    for i in k:
        n.append(int(i))

It is more verbose and easier to read and they both do exactly the same 
thing!

iPython is great, to install it you might try easy_install:
http://peak.telecommunity.com/DevCenter/EasyInstall

Then in a command line type:
easy_install ipython

Then, once it is complete, to use iPython type:
ipython

--
Andrew



More information about the Python-list mailing list