-1

我是 python 新手。我想问一下如何在python中解码一个数据数组,例如

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

我希望输出如下:

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

在 python 中使用哪个函数?类似verilog中的案例谢谢!

4

1 回答 1

0

你所描述的可以通过 Python 的dictionary类型来完成。


# 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!']

于 2021-06-21T16:15:50.160 回答