-2

I want to create a look-up table in Python that associate integers to a series of symbols. The symbols that I want to use are small letters [a-z] and numbers [0-9].

My goal is to represent a large amount numbers with the fewest symbols possible. I want to know what is the most efficient way to do this in Python?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2776193
  • 103
  • 3
  • 9
  • 1
    Are you effectively talking about using a base-36 number system, i.e. like hexadecimal but with a-z instead of just a-f? – jonrsharpe Jun 22 '14 at 09:57
  • 1
    Take a look at http://stackoverflow.com/questions/1181919/python-base-36-encoding – Iftah Jun 22 '14 at 09:59
  • thanks to `Iftah` I found what I was looking for here [a link](http://stackoverflow.com/questions/715251/out-of-curiosity-how-are-serial-numbers-generated-hints-algorithms) – user2776193 Jun 22 '14 at 10:13

1 Answers1

2

If your integers are positive, contiguous, and start at or close to 0, use a list. Otherwise, use a dictionary. If your target values are single characters, you can use a string instead of a list.

Lists (and strings) are more efficient at mapping a sequence (0, 1, 2, etc.), dictionaries are more efficient at mapping disparate numbers to values.

Mapping integers to single characters:

targets = "a8B2Vgj4l" # targets[3] -> 'B'

Mapping integers to multiple characters:

targets = ['foo', 'bar', 'baz', '42andahalf'] # targets[3] -> 'baz'

Mapping noncontiguous integers:

targets = {42: 'Life', 81: 'Monty Python'}  # targets[82] -> 'Monty Python'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343