# Converting List of String to Integer

14 messages
Open this post in threaded view
|

## Converting List of String to Integer

 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
Open this post in threaded view
|

## Re: Converting List of String to Integer

 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 -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 On Jul 21, 3:20 pm, Gary Herron <[hidden email]> 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
Open this post in threaded view
|

## Re: Converting List of String to Integer

 On Jul 22, 6:11 am, Samir <[hidden email]> wrote: [snip] > 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]) To see what is happening, insert some print statements, plus something to slow it down e.g. for k in a:     print id(a), a     print id(n), n     n.append([int(v) for v in k])     raw_input('Hit Enter to continue ->') > > print n > -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 Samir wrote: > On Jul 21, 3:20 pm, Gary Herron <[hidden email]> 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? > >   Aha.  There's the problem, right there in the first line. > a = n = [] >   This sets a and n to the *same* empty list.    This line creates one empty list and binds both n and a to that list.  Note carefully,  there is only one empty list here, but it can be accessed under two names Later in your code,   for k in a: runs through that list, and   n.append(...) append to the end of the same list.  Thus the loop never get to the end of the (continually growing) list. Solve it by creating two different empty lists:   a = []   n = [] Gary Herron > 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>   -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 Samir wrote: > On Jul 21, 3:20 pm, Gary Herron <[hidden email]> 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-listI 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 -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 On Jul 21, 6:15 pm, Andrew Freeman <[hidden email]> wrote: > Samir wrote: > > On Jul 21, 3:20 pm, Gary Herron <[hidden email]> 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
Open this post in threaded view
|

## Re: Converting List of String to Integer

 Samir wrote: > On Jul 21, 6:15 pm, Andrew Freeman <[hidden email]> wrote: >   >> Samir wrote: >>     >>> On Jul 21, 3:20 pm, Gary Herron <[hidden email]> 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/EasyInstallThen in a command line type: easy_install ipython Then, once it is complete, to use iPython type: ipython -- Andrew -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 Samir wrote: > 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. >   About speed, and memory consumption: List comprehensions (http://docs.python.org/tut/node7.html#SECTION007140000000000000000) are just shortcuts for for-loops. I do not believe there is any speed benefit. However, there are generators, they basically load one part of an iterator (a list in this case) at a time, this can greatly reduce memory usage. Have a look at PEP 289: http://www.python.org/dev/peps/pep-0289/Here is the list comprehension as a generator (actually 2): n = ((int(i) for i in k) for k in a) Note, you can't just print a generator, it only computes something when needed:  >>> print n You can, however iterate over it: In [2]: for k in n:    ....:     for i in k:    ....:         print i,    ....:            ....:         1 2 3 4 5 6 7 8 9 0 In [3]: n = ((int(i) for i in k) for k in a) In [49]: list(n) Out[49]: [,  ,  ,  ] Each sub-list is a generator too! In [50]: n = ((int(i) for i in k) for k in a) In [51]: for i in list(n): # list() converts the variable n to a list    ....:     list(i)    ....:        ....:     Out[51]: [1, 2] Out[51]: [3] Out[51]: [4, 5, 6] Out[51]: [7, 8, 9, 0] This is only going to make a difference if you were dealing with a *very* large data set. I thought I would show you even if you never user them, for learning purposes. Note: a generator is one way, redefine it every time you use it. -- Andrew -- http://mail.python.org/mailman/listinfo/python-list
Open this post in threaded view
|

## Re: Converting List of String to Integer

 In reply to this post by Samir-16 >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']] >>> [map(int, i) for i in a] [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]] On Jul 21, 9:06 pm, Samir <[hidden email]> 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
Open this post in threaded view
|

## Re: Converting List of String to Integer

 Just to throw my hat in the ring, this is another way you can do this:[(lambda x : [int(ii) for ii in x])(y) for y in a]However, I do think dusans way is more elegant.Best, ROn Tue, Jul 22, 2008 at 4:58 PM, dusans <[hidden email]> wrote: >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']] >>> [map(int, i) for i in a] [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]] On Jul 21, 9:06 pm, Samir <[hidden email]> 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 -- http://mail.python.org/mailman/listinfo/python-list