首页 > 解决方案 > 从子列表列表中删除索引以返回它

问题描述

涉及很多功能,但我会保持简单:

这是我的代码:

[['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.

标签: pythonloopsindexing

解决方案


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

推荐阅读