-3

I am trying to match the letters on the alphabet "A-Z" with numbers 1-26 and the numbers 0-9 with 48-57. I am aware I could accomplish the alphabet part with ASCII tables but I was wondering if there is a quick or easy way in Python to define this behaviour:

  • Define a list (for easy understanding let's call this List1) of letters and numbers.
  • Define a list (List2) of numbers
  • Assing each item on the first list with the item on the same position on the second list.

The idea is to be able to pass a list of numbers (List2) and return their correspondent item on List1.

This is to decode ADS-B data for plane identification. More info here.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
emilio-cea
  • 37
  • 5
  • 3
    What have you tried, and what exactly is the problem with it? – jonrsharpe Aug 12 '20 at 17:11
  • Does this answer your question? [How to get the ASCII value of a character](https://stackoverflow.com/questions/227459/how-to-get-the-ascii-value-of-a-character) – Pranav Hosangadi Aug 12 '20 at 17:12
  • don't use multiple lists but a dictionary – rioV8 Aug 12 '20 at 17:21
  • Some pieces you'll want: the `string` module, `itertools.chain`, the fact that ASCII was defined so that `ord('A') & 63 == 1`, and a list or dict comprehension. – chepner Aug 12 '20 at 17:21
  • Thanks for the feedback and sorry for the late reply, busy day! Maybe a dictionary is a better solution and as I can see from the answer below is the correct way to go. Thanks again! – emilio-cea Aug 13 '20 at 09:32

1 Answers1

0

Define a list (for easy understanding let's call this List1) of letters and numbers.

>>> import string
>>> list1 = string.ascii_uppercase + string.digits
>>> list1
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

Define a list (List2) of numbers.

I am trying to match the letters on the alphabet "A-Z" with numbers 1-26 and the numbers 0-9 with 48-57.

>>> list2 = list(range(1,27))+list(range(48,58))
>>> list2
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]

Assign each item on the first list with the item on the same position on the second list.

This will create a dictionary of numbers to characters:

>>> table = dict(zip(list2,list1))
>>> table
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'Y', 25: 'X', 26: 'Z', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6', 55: '7', 56: '8', 57: '9'}

The idea is to be able to pass a list of numbers (List2) and return their correspondent item on List1.

>>> def lookup(L):
...    return ''.join([table[x] for x in L])
...
>>> lookup([1,2,3,48,49])
'ABC01'
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251