List mapping question
Steve Holden
steve at holdenweb.com
Thu Feb 3 17:17:51 EST 2005
Marc Huffnagle wrote:
> I have a number of variables that I want to modify (a bunch of strings
> that I need to convert into ints). Is there an easy way to do that
> other than saying:
>
> > a = int(a)
> > b = int(b)
> > c = int(c)
>
> I tried
>
> > [i = int(i) for i in [a, b, c]]
>
> but that didn't work because it was creating a list with the values of
> a, b and c instead of the actual variables themselves, then trying to
> set a string equal to an integer, which it really didn't like.
>
> Marc
>>> a,b,c = 1.1, 2.2, 3.3
>>> a,b,c = map(int, (a,b,c))
>>> a,b,c
(1, 2, 3)
>>> a,b,c = [int(x) for x in (a,b,c)]
>>> a,b,c
(1, 2, 3)
regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
More information about the Python-list
mailing list