Converting List of String to Integer

Rishabh Manocha rmanocha at gmail.com
Tue Jul 22 08:01:59 EDT 2008


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,

R

On Tue, Jul 22, 2008 at 4:58 PM, dusans <dusan.smitran at gmail.com> 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 <spytho... at gmail.com> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080722/bc5579cf/attachment-0001.html>


More information about the Python-list mailing list