我知道这可能是一个愚蠢的问题,但我有一个方法,就是移动列表中的一个项目从一个索引到另一个。(此列表是音乐歌曲的队列。。。不太相关)我想输入1,它与列表的第一个索引相关,依此类推。(基本上,将move_from
和move_to
输入移到左边,因为这是供人可读的输入)
方法如下
self.queue = [... assume has songs queued ...]
async def move(self, move_from: int, move_to: int):
#I'm not doing any error checking here but assume it's being done
move_from -= 1; move_to -= 1 #semicolon should be correct here for doing multiple assignments in one line correct? why do I second guess myself :D
item = self.queue[move_from]
self.queue.insert(move_to, self.queue.pop(move_from))
print(f"Moved {item.name} from position {move_from+1} to position {move_to+1}")
好吧,我知道我还在print语句中添加一个值(这似乎有些多余,让我有点恼火,但这是我个人的事情)但是我该如何改变语句中的第一行,一次减少两个值,而不是分别进行呢。你知道吗
奖金
I have a bonus question as well (that I'm asking out of haste as I have not tried it yet but want to know if I'm correct):
我想知道我是否可以在我的方法定义中这样做:
async def move(self, move_from: int = lambda x:x-=1, move_to: int = lambda y:y-=1):
这合法吗?如果是这样,我觉得我做错了。你知道吗
编辑
有没有办法让列表索引从1开始而不是从0开始?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐