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 3.x 将特定类的元素列表转换为以对象属性为键和值的字典_Python 3.x_List_Dictionary - Fatal编程技术网

Python 3.x 将特定类的元素列表转换为以对象属性为键和值的字典

Python 3.x 将特定类的元素列表转换为以对象属性为键和值的字典,python-3.x,list,dictionary,Python 3.x,List,Dictionary,我有一个对象列表,每个对象都有一个名为key的属性和另一个名为value的属性 mylist[0].key = "1" mylist[0].value = "one" mylist[1].key = "2" mylist[1].value = "two" mylist[2].key = "3" mylist[2].value = "three" 如何将其转换为以下词典 mydict = {"1": "one", "2": "two", "3": "three"} 使用听写理解 例: 像这样使

我有一个对象列表,每个对象都有一个名为key的属性和另一个名为value的属性

mylist[0].key = "1"
mylist[0].value = "one"
mylist[1].key = "2"
mylist[1].value = "two"
mylist[2].key = "3"
mylist[2].value = "three"
如何将其转换为以下词典

mydict = {"1": "one", "2": "two", "3": "three"}

使用听写理解

例:


像这样使用字典理解-

mydict = {obj.key : obj.value for obj in mylist}
此列表是一个对象列表

mydict = {obj.key : obj.value for obj in mylist}