如何解决python - 将数据数组解码为预定义值
我是 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 中的案例 谢谢!
解决方法
您所描述的可以通过 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!']
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。