-1

I am new to python. I would like to ask how to decode an array of data in python, for example

[ 1 2 3 4 5  6  7 8 9 10 4 3 2 4 11 12 13 14 3 2 1 3] 

and I wanted the output to be like:

if data = 1, data become predefined value A
if data = 2, data become predefined value B
... 
if data = 16, data become predefined value X

Which function to use in python ? something like case in verilog Thank you !

aminrd
  • 4,300
  • 4
  • 23
  • 45

1 Answers1

0

What you have describe can be done by Python's dictionary type.


# decoder
numbers_to_words = {1: 'hey', 2: 'this', 3: 'python', 4: 'dictionary', 5: 'is', 6: 'so', 7: 'cool!'}
L = [1, 2, 3, 4, 5, 6, 7]

for index, data in enumerate(L):
    # here, the current data is replaced by its corresponding decoder value.
    L[index] = numbers_to_words[data]

print(L)
['hey', 'this', 'python', 'dictionary', 'is', 'so', 'cool!']

Ibrahim Berber
  • 842
  • 2
  • 16