python - 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!']
推荐阅读
- android - 在数据绑定中找不到属性的设置器
- angular - ionic 3 应用程序中的更改检测问题
- yii2 - afterSave() 中的 $changedAttributes 是否与 $this->getDirtyAttributes() 相同?
- python - 使用外部库或参数在 PyCharm 中运行 python 文件
- python - pandas groupby 聚合逐元素列表添加
- vba - 删除特定范围行的宏
- jquery - jQuery 使用 ajax 输入创建表 - 我希望一列能够让用户更改内容
- jenkins - 如何在 Jenkins 中获取当前登录用户?
- python - 在 Python 中绘制两个正弦曲线的总和
- maven-plugin - 如何将“纱线运行脚本构建”任务从前端 maven 插件迁移到 gradle?