Multiway Branching

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Jan 8 14:43:16 EST 2006


A dict can be useful:

byte1, byte2 = 32, 1

conv1 = {(32, 32):0, (36,32):"natural", (32,1):5, (66,32):0.167}
print conv1[byte1, byte2]

If you use Psyco maybe something like this can be faster:

conv2 = dict((k1*256+k2,v) for (k1,k2),v in conv1.items())
print conv2[(byte1<<8) + byte2]

conv1/conv2 has to be defined just one time.

Bye,
bearophile




More information about the Python-list mailing list