python - how to extract the first element of each tuple in each sublist?
问题描述
I have a list of lists and within each sublist there are tuples with the format (iD, volume). I need to keep the first element of each tuple and do away with the second while saving the new list of lists into binContents
.
for example:
bins = [[(2, 22), (1, 13)], [(2, 22)], [(0, 20)]]
binContents =
Desired outcome:
print(binContents)
[[2,1],[2],[0]]
*not a duplicate of How to make a flat list out of list of lists? because I do not intend on making a flat list, and that code with additional indexing did not give me my desired result
解决方案
开始了:
bins = [[(2, 22), (1, 13)], [(2, 22)], [(0, 20)]]
binContents = [[y[0] for y in x] for x in bins]
print(binContents)
这产生
[[2, 1], [2], [0]]
推荐阅读
- python - ModelChoiceFilter 给出“选择一个有效的选择。该选择不是可用的选择之一。” 提交时
- triggers - 将触发器的名称设置为 Google 跟踪代码管理器上的事件操作
- javascript - JS - 拦截所有传入的 XML 和 Websocket 请求客户端
- vue.js - Vue:观察者可以用来观察计算属性吗?
- ionic-framework - 离子存储集法
- spring-boot - Spring-boot登录常用方法
- keras - Keras 训练输出中的“s/it”是什么意思?
- python - 创建python.exe文件时python中的数学函数不起作用
- postgresql - Postgres - 在 github 操作中运行 \c 命令
- python - 使用python从大表中删除大量记录的有效方法