有没有一种方法可以一次添加多个变量?

时间:2018-12-14 09:46:17

标签: python

我知道这可能是一个愚蠢的问题,但是我有一种方法可以将列表中的项目从一个索引移动到另一个索引。 (此列表是音乐歌曲的队列...不太相关)我希望能够输入1,并且与列表的第一个索引相关,依此类推。 (基本上,将move_frommove_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}")

好吧,所以我知道我仍在我的打印语句中添加一个(这看起来很多余,有点烦我,但这是个人的事情),但是我将如何更改第一个在我的语句中添加一行,以立即减少两个值,而不必分别进行处理。

奖金

  

我也有一个奖励问题(由于我还没有尝试过,但我急于要问,但想知道我是否正确)

我想知道是否可以在我的方法定义中做到这一点:

async def move(self, move_from: int = lambda x:x-=1, move_to: int = lambda y:y-=1):

这合法吗?如果是这样,我觉得我做错了。

修改

有没有办法使列表索引从1开始而不是从0开始?

0 个答案:

没有答案