python - 您如何将此循环编写为列表理解?
问题描述
我有这个代码
lista = list()
for dict_genres in df['genres']:
suport = list()
for genres in dict_genres:
suport.append(genres['id'])
lista.append(suport)
您如何将其写为列表理解?
解决方案
具有 1 级列表理解
lista = []
for dict_genres in df['genres']:
lista.append([genres['id'] for genres in dict_genres])
具有 2 级列表理解
lista = [
[genres['id'] for genres in dict_genres]
for dict_genres in df['genres']
]
推荐阅读
- c# - 抑制 DNN 中的默认 XFrameOptions
- html - 将弹性项目设置为 100% 高度
- python-3.x - PiZero W 连接到 (GPIO):如何在 Mac OS X 上使用 GPIO 输入模拟 PiZero
- c - C:提取字符串中的两个浮点数
- c# - 在 netcore api 3.1 中读取 AuthorizationFilterContext
- php - 在PHP中不断改变数字字母编号?
- javascript - 按类名抓取元素,返回大小为 0 的数组
- typescript - 如何查看 TypeScript 是如何计算类型的?
- r - map2 在两个列表上并在新列表中创建一个矩阵
- perl - 在 Perl 中读取和文本匹配 Outlook .msg 文件