如何在混合状态下对列表进行排序

时间:2019-05-07 22:24:20

标签: python-3.x algorithm sorting pyqt5

我有一个列表,其中包含歌曲名称和下一首歌曲的按钮,我混合了列表并尝试执行以下操作。 当我按下一个键时,我的功能会产生一个带有按钮名称的字符串,以及一首将要播放的歌曲,但是在大多数情况下,我大约有3首不同的歌曲,然后同一首歌曲的重复开始几次,所以我创建了一个清单检查其中是否有歌曲,然后再播放另一首歌曲,但是由于我有很多条件,我无法弄清楚如何在代码中实现它。告诉我如何解决此问题或解决此问题的另一种方法。

def initUI(self):
     btn1.clicked.connect(self.buttonClicked)

 def get_normal(self):
        #standart list
        a=os.listdir(path='D:/python/phil4es-music_player-c8a23f947bc1/music')
        #mixed list
        b =os.listdir(path='D:/python/phil4es-music_player-c8a23f947bc1/music')
        self.answer = a
        self.b_answer= b

 def buttonClicked(self):
        self.full_list=[]
        self.end_list=-1
        self.end_list_plus=0
        self.ready_list=[]
        self.prev_music=None
        sender = self.sender()
        self.pos_index=0
        random.shuffle(self.b_answer)
        self.now_play=self.answer[0]
        for i in range(len(self.answer)):
            self.now_play_inb = self.b_answer[self.pos_index]
            if self.now_play==self.now_play_inb:
                self.prev_music=self.b_answer[self.pos_index+self.end_list_plus]
                self.now_play=self.b_answer[self.pos_index+1]
                self.full_list.append(self.now_play)
                self.pos_index+=1
                self.end_list_plus+=2
                if self.prev_music == self.now_play:
                    self.now_play=self.b_answer[self.pos_index + 2]
                    self.full_list.append(self.now_play)
                    self.pos_index+=2
                    self.end_list_plus+=2
                    if self.pos_index==len(self.answer):
                        self.now_play=self.answer[0]
                        self.full_list.append(self.now_play)
                        if self.pos_index>len(self.answer):
                            self.pos_index = 0
                            self.end_list_plus = -1

            if self.now_play!=self.now_play_inb:
                self.prev_music=self.b_answer[self.end_list+self.end_list_plus]
                self.now_play=self.b_answer[self.pos_index]
                self.full_list.append(self.now_play)
                self.pos_index+=1
                self.end_list_plus+=1
                if self.prev_music == self.now_play:
                    self.now_play=self.b_answer[self.pos_index+1]
                    self.full_list.append(self.now_play)
                    self.pos_index+=1
                    self.end_list_plus+=1
                    if self.pos_index==len(self.answer):
                        self.now_play=self.answer[0]
                        self.full_list.append(self.now_play)
                        if self.pos_index>len(self.answer):
                            self.pos_index = 0
                            self.end_list_plus = -1
             return self.statusBar().showMessage(sender.text() + self.now_play)

0 个答案:

没有答案