List mapping question

M

Marc Huffnagle

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
 
S

Steve Holden

Marc said:
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
 
M

Marc Huffnagle

Steve said:
Marc said:
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

Thanks ... so there's no way to pass an actual variable into a list
mapping, instead of its value? I guess I'm thinking of something the
equivalent of call by reference.


Marc
 
S

Steven Bethard

Marc said:
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.

Actually, that didn't work because it's a SyntaxError to have an
assigment statement inside a list comprehension:

py> [i = int(i) for i in [a, b, c]]
Traceback ( File "<interactive input>", line 1
[i = int(i) for i in [a, b, c]]
^
SyntaxError: invalid syntax

You could try something like:

py> a, b, c
('1', '2', '3')
py> a, b, c = [int(i) for i in (a, b, c)]
py> a, b, c
(1, 2, 3)

For a few variables, this is probably a reasonable solution. For more
than 4 or 5 though, it's going to get unreadable. Of course for that
many variables, you should probably be keeping them in a list instead of
as separate names anyway...

Steve
 
S

Steven Bethard

Marc said:
Steve said:
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

Thanks ... so there's no way to pass an actual variable into a list
mapping, instead of its value? I guess I'm thinking of something the
equivalent of call by reference.

No, not really. You could do something like:

py> a, b, c = 1.1, 2.2, 3.3
py> mod = __import__(__name__)
py> for name in ('a', 'b', 'c'):
.... setattr(mod, name, int(getattr(mod, name)))
....
py> a, b, c
(1, 2, 3)

where you use the namespace in which the name resides (the module), but
I'd probably advise against it... I would suggest doing something like:

py> myvars = dict(a=1.1, b=2.2, c=3.3)
py> for key, value in myvars.iteritems():
.... myvars[key] = int(value)
....
py> myvars
{'a': 1, 'c': 3, 'b': 2}

If you want to pass around variables, it's generally better to put them
in a dict, list, class instance or some other sort of container then
mess around with them at the module level...

(another) STeVe
 
M

Michael Spencer

Marc said:
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:

It may not matter to you, at the moment, but a = int(a) is not strictly
'modifying a variable'. Instead int(a) creates a new int object, if possible,
from the object that a is currently bound to. Then a is rebound to the new object.
I tried
[i = int(i) for i in [a, b, c]]

You can't make an assignment in a list comprehension. If your 'variables' are
object attributes, you could do: [setattr(obj,name,int(getattr(obj,name)) for
name in [list of attribute names]]

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

For your problem as stated:
>>> a=b=c="1"
>>> for var in ["a","b","c"]:
.... exec "%s = int(%s)" % (var,var)
....
But don't do this, except as a "one-off" data crunching exercise

Michael
 
C

Christos TZOTZIOY Georgiou

Thanks ... so there's no way to pass an actual variable into a list
mapping, instead of its value? I guess I'm thinking of something the
equivalent of call by reference.

No, there is no way (the way you mean it). Python variables are 'names', not
'lvalues'. Everything in Python is an object, and you just name objects.

By doing

a = 1

you are just giving the name 'a' in the current namespace to the integer object
with value 1.

PS this might confuse you, but in python, all function calls are by value, and
all values passed are references to objects.

http://groups-beta.google.com/groups?q=group:comp.lang.python+value+reference
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top