python - 从子列表列表中删除索引以返回它
问题描述
涉及很多功能,但我会保持简单:
这是我的代码:
[['Musique', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Musique', 'The Singing Pianos', 216, 216], ['Musique', 'Rythmes et détente : Duo Pichenotte', 216, 216]]
我只想将每个子列表的索引 [1] 作为字符串返回。它是法语,但索引 [1] 是每个子列表的标题。每个子列表都是一个事件,我只需要返回名称。我的代码中实际上有更多事件,但我想要一个简单的代码,并且我会尽力而为。
因此,如果我们正在查看我给您的代码示例,我将不得不返回:
Initiation au tango argentin suivi de la milonga
The Singing Pianos
Rythmes et détente : Duo Pichenotte
如果有一种方法可以像我的退货示例那样在不同的线路上退货,那也很棒。
我试过的:
我很难在子列表列表中使用索引。仅将标题作为每个列表的 str 返回是很重要的。我尝试使用一段时间
while i < len(events):
print(events[i][:1][0:1]) # That would search every index i need, right ?
but it didnt work. there is more code involved but you get the picture and i dont want to add 8 functions to this scenario.
解决方案
l=[['Musique', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Musique', 'The Singing Pianos', 216, 216], ['Musique', 'Rythmes et détente : Duo Pichenotte', 216, 216]]
然后试试这个:
print('\n'.join([i[1] for i in l]))
或者然后:
print('\n'.join(list(zip(*l))[1]))
或者然后(numpy):
import numpy as np
l2=np.array(l)
print('\n'.join(l2[:,1].tolist()))
所有输出:
Initiation au tango argentin suivi de la milonga
The Singing Pianos
Rythmes et détente : Duo Pichenotte
推荐阅读
- python - VBA 无法在新 PC 上运行 python 脚本
- python-3.x - 我正在使用 tkinter 开发游戏。犯一个愚蠢的错误
- javascript - 在CKEditor 5上插入图像时如何获取输入alt-title
- javascript - Vue JS联系表单错误“无法读取未定义的属性'post'”
- vb6 - 不确定如何填充用户定义的类型记录/元素
- r - 如何将字符列表向量组合成一个字符向量,同时保持每个列表的内容不变
- node.js - 获取基于数组的长度 - 猫鼬
- python - 带管道的单线输出
- python-3.x - 如何在 python 中将 datetime.datetime(2020, 3, 5, 18, 55, tzinfo=tzoffset(None, 19800)) 转换为 2019-01-08 11:49:00.000000?
- ios - 在多个视图控制器中使用相同的表格视图单元