Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将整数赋给字符串_Python_String_Variables_Integer - Fatal编程技术网

Python 将整数赋给字符串

Python 将整数赋给字符串,python,string,variables,integer,Python,String,Variables,Integer,我在Python中工作,我有一个整数列表:[1,2,1,3,2,2,1,3] 我想给每个整数分配一个字符串,就像它是一个变量一样: [‘红色’、‘橙色’、‘红色’、‘黄色’、‘橙色’、‘红色’、‘黄色’] 我该怎么做呢 在该示例中,1对应于'red',2对应于'orange',3对应于'yellow' 谢谢 使用字典 d = {1: 'red', 2: 'orange', 3: 'yellow'} 然后,您可以执行以下操作来更改列表: lst = [d[k] for k in lst] 字典

我在Python中工作,我有一个整数列表:
[1,2,1,3,2,2,1,3]

我想给每个整数分配一个字符串,就像它是一个变量一样:
[‘红色’、‘橙色’、‘红色’、‘黄色’、‘橙色’、‘红色’、‘黄色’]

我该怎么做呢

在该示例中,
1
对应于
'red'
2
对应于
'orange'
3
对应于
'yellow'

谢谢

使用字典

d = {1: 'red', 2: 'orange', 3: 'yellow'}
然后,您可以执行以下操作来更改列表:

lst = [d[k] for k in lst]

字典基本上将对象(在本例中是整数)映射到其他对象,这正是您想要的。

您想在python中实现类似于C enum的东西吗?