python - 将 python 列表转换为单项字典列表
问题描述
我试图找出将列表转换为单项词典列表的最有效方法,例如:
my_fruit = [
'apple',
'orange',
'pear',
'grape'
]
至
my_fruit = [
{'apple': 'apple'},
{'orange': 'orange'},
{'pear': 'pear'},
{'grape': 'grape'}
]
我只是对 Python 不够熟悉,无法找出最好的方法。看起来应该很容易。
解决方案
my_fruit = [{x: x} for x in my_fruit]
将创建所需的结构。不过,该结构的目的还不是很清楚;-)
推荐阅读
- javascript - 有没有办法让 CSS 动画在动画对象不可见之前不运行?
- c# - 如何检查我的 .NET Core 控制台应用程序是作为独立应用程序运行(通过运行 myapp.exe)还是使用 dotnet myapp.dll 运行?
- python - Django 条件选择
- sql - Postgresql - 从值生成一个百分比,其中 100% 是一个特定值和几天的平均值
- c++ - 读取一个整数并打印所有小于给定整数的完美数
- omnet++ - getleader() 的 Omnet++ 问题
- c++ - 为什么类中的参数从一个函数更改为另一个函数?
- php - 在创建后设置自定义选择字段的默认值(wordpress PHP jquery)
- azure-devops - Azure DevOps:预览体验中不支持资源类型“microsoft.visualstudio/account”
- gulp - Gulpfile watchTask 仅在第一次运行时