Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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/1/list/4.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_List_Enums_Mergesort - Fatal编程技术网

Python 如何将值列表转换为枚举的名称列表?

Python 如何将值列表转换为枚举的名称列表?,python,list,enums,mergesort,Python,List,Enums,Mergesort,假设我有一个列表,我使用mergeSort对它进行排序。如何转换已排序的列表,以便看到动物的名称而不是值 theList = [Animal.Bear.value,Animal.Ant.value,Animal.Lion] mergeSort(theList) print(theList) 您可以浏览列表,并通过枚举构造函数将值转换为名称: nameList = [Animal(x).name for x in theList]; 据我所知,Python中没有mergeSort函数,如果

假设我有一个列表,我使用
mergeSort
对它进行排序。如何转换已排序的列表,以便看到动物的名称而不是值

theList = [Animal.Bear.value,Animal.Ant.value,Animal.Lion]

mergeSort(theList)

print(theList)

您可以浏览列表,并通过枚举构造函数将值转换为名称:

nameList = [Animal(x).name for x in theList];

据我所知,Python中没有
mergeSort
函数,如果有的话,它必须在某个单独的库中。这里没有足够的信息来回答这个问题,请给出一个。
Animal.Bear.value
通常也不存在。几乎可以肯定的是,这背后有很多你自己的代码,我们看不到任何一个。@roganjosh这个标题至少提到了
enum
,我想这意味着有一个类似
类动物(enum)的定义:熊=1蚂蚁=2狮子=3
。如果你使用
sort
,我建议首先不要存储值列表<代码>列表=[Animal.Bear,Animal.Ant,Animal.Lion];list.sort(key=lambda x:x.value)@chepner good point。我不确定这是否会使我的评论变得毫无意义,因为(尽管可能是正确的)我们没有得到一个清晰的图片,谢谢。我找到了另一种方法:)