Reputation:
How can i swap the value for all attribute?? I want FieldIP = 1. MomentIP=1 FieldOP =3 and so on.
FieldIP =3 FieldOP =1
MomentIP =3 MomentOP =1
NormalizeMomentIP =3 NormalizeMomentOP =1
ixfwdIP =3 ixfwdOP =1
iyfwdIP =3 iyfwdOP =1
ixbwdIP =3 ixbwdOP =1
iybwdIP =3 iybwdOP =1
MsAvgIP =3 MsAvgOP =1
MsStdIP =3 MsStdOP =1
HkIP =3 HkOP =1
NegFieldIP =3 NegFieldOP =1
NegNormalizeMomentIP =3 NegNormalizeMomentOP =1
NegFieldBotIP =3 NegFieldBotOPx` =1
NegNormalizeMomentBotIP =3 NegNormalizeMomentBotOP =1
How can i swap the value for all attribute?? Is there any shorter way? This is the code i did. If i did finish all 14 attribute it will be very long as i have to declare 28 variable...
x = FieldIP
y = FieldOP
...
if ( HkOP > HkIP):
FieldOP = x
FieldIP = y
print "Swapped"
FieldIP =1 FieldOP =3
MomentIP =1 MomentOP =3
NormalizeMomentIP =1 NormalizeMomentOP =3
ixfwdIP =1 ixfwdOP =3
iyfwdIP =1 iyfwdOP =3
ixbwdIP =1 ixbwdOP =3
iybwdIP =1 iybwdOP =3
MsAvgIP =1 MsAvgOP =3
MsStdIP =1 MsStdOP =3
HkIP =1 HkOP =3
NegFieldIP =1 NegFieldOP =3
NegNormalizeMomentIP =1 NegNormalizeMomentOP =3
NegFieldBotIP =1 NegFieldBotOPx` =3
NegNormalizeMomentBotIP =1 NegNormalizeMomentBotOP =3
Upvotes: 0
Views: 58
Reputation: 2629
I'm assuming this data of yours is in a text file of some sort.
Try something like this:
import re
# read your data
lines = open('mytable.txt').readlines()
# regex to capture your 'values'
regex = re.compile(r'(.*?=)([0-9]+)(.*?=)([0-9]+)')
for line in lines:
# for each line, match the value groups and substitute swapped values
print regex.sub(r'\1\4\3\2', line)
Should print the table with swapped values, and preserve the whitespace too.
Upvotes: 1
Reputation: 5999
You can iterate through local vars to do so:
key = key2 = None
for key in locals():
if key.endswith('OP'):
key2 = key[:-2] + 'IP'
if locals()[key] > locals()[key2]:
locals()[key], locals()[key2] = locals()[key2], locals()[key]
print 'swapped'
You have to declare key
and key2
before the for loop, not inside it.
Upvotes: 0