[Tutor] Converting string or list to sum
Iain
python at cairnlin.co.uk
Thu Jan 29 15:47:03 EST 2004
Thanks,
I think I can put two ideas together to solve it for lists :
>>> a=[2, 3, '+', 4]
>>> d=""
>>> for item in range(len(a)):
... d=d+str(a[item])
...
>>> d
'23+4'
>>> eval(d)
27
I didn't realise eval did that - very handy.
Iain.
> On Thursday 29 January 2004 19:47, orbitz at ezabel.com wrote:
> > The easiest way out is probably to use eval.
> >
> > >>> eval('2+1')
> >
> > 3
> >
> >
> > if you have a list, however, you will just create a list using that.
> >
> >
> > On Thu, 29 Jan 2004 19:14:23 +0000
> >
> > Iain <python at cairnlin.co.uk> wrote:
> > > If I have a sum as a string (e.g. '2+3') or in a list (e.g. [2,'+',3]),
> > > how can I get the result of the sum?
> > >
> > > Thanks
> > >
> > > Iain.
> > >
> > >
> > > _______________________________________________
> > > Tutor maillist - Tutor at python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list