从子列表中删除索引以返回i

2024-04-26 15:12:13 发布

您现在位置: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

如果有一种方法可以把它们放在不同的行上,比如我的return示例,那也太好了

我尝试过:

我很难在子列表中使用索引。只返回每个列表的标题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.

Tags: the代码列表delaautangosinging
1条回答
网友
1楼 · 发布于 2024-04-26 15:12:13
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

相关问题 更多 >