I have a problem that seems like it should be so simple but I can't work it out. I have this section of code:
Avalue = '2:7'
Bvalue = '4?3'
Cvalue = '5\t8'
for x in [Avalue,Bvalue,Cvalue]:
for illChar in ['?','/','|','*','<','>',':','\n','\t','"']:
if illChar in x:
x = x.replace(illChar, "")
int(x)
print type(x)
print type(Avalue)
OUTPUT:
<type 'int'>
<type 'int'>
<type 'int'>
<type 'unicode'>
What I want to do is to strip out any of the illegal characters, and then change Avalue,Bvalue,Cvalue to integers rather than strings, so that Avalue becomes 27 for example.
But the above code doesn't actually change the original variable, it just changes the value of the variable x within that list which takes it's value from the original value of Avalue (or Bvalue or Cvalue etc). Now, I know I could just change each original variable individually, i.e. Avalue = int(Avalue)
, but if I end up having a whole load of variables to change it would seem more elegant to use a loop.
So, is there a way to alter variables by using a for loop like this?
edit: ok sorry I may have caused confusion with the example above, perhaps this might make more sense. Suppose I have the following:
Avalue = 'hello'
Bvalue = 'my name'
Cvalue = 'is'
for x in [Avalue,Bvalue,Cvalue]:
x = 'slimshady'
print Avalue
The above doesn't work, Avalue remains as 'hello'. Suppose I wanted Avalue Bvalue Cvalue all to be permanently changed to 'slimshady' can I do that using a loop?