问题描述
我有一个支持播放列表的音乐机器人。为此,我创建了一个 playlist
命令来显示队列。我还有一个清除完整队列的命令。现在我想实现从列表中按编号删除一首歌曲的可能性。我将如何重写以下命令?
@commands.cooldown(1,5,BucketType.guild)
@commands.command(aliases=["delplaylist"])
@commands.guild_only()
@commands.check(audio_playing)
async def remove(self,ctx):
"""Deletes a single song from the playlist."""
state = self.get_state(ctx.guild)
entries = []
for (index,song) in enumerate(state.playlist):
entries.append(
(f"**[{index + 1}] {song.title}** *by* {song.uploader}",f"requested by {song.requested_by.mention}"))
pages = PlaylistPages(ctx,entries)
return await pages.paginate()
我知道代码只显示播放列表本身。我会先获取 entries
并按数字删除它们,但这是否可能以这种“简单”的方式进行?
解决方法
按索引删除:
使用 del
keyword
>>> lst = ['elem1','elem2','elem3','elem4','elem5']
>>> del lst[0] # The index of the element
>>> lst
['elem2','elem5']
使用 list.pop
>>> lst = ['elem1','elem5']
>>> lst.pop(0) # It also returns the value
'elem1'
>>> lst
['elem2','elem5']