首页 > 解决方案 > 将 python 列表转换为单项字典列表

问题描述

我试图找出将列表转换为单项词典列表的最有效方法,例如:

my_fruit = [
   'apple', 
   'orange',
   'pear',
   'grape'
]

my_fruit = [
   {'apple': 'apple'},
   {'orange': 'orange'},
   {'pear': 'pear'},
   {'grape': 'grape'}
]

我只是对 Python 不够熟悉,无法找出最好的方法。看起来应该很容易。

标签: pythonpython-3.x

解决方案


my_fruit = [{x: x} for x in my_fruit] 

将创建所需的结构。不过,该结构的目的还不是很清楚;-)


推荐阅读