jeudi 16 mars 2017

How to find item index after selecting randomized item from array

Trying to get something to work where I randomize 4 objects in an array and randomly select one of those. I need to be able to get the original index number for that chosen object back. Any idea on how I should write this as short as possible?

arrayRandomSongs = []
arrayChosen = []
trackChosen = ""

def randomizeArray(self):
    del self.arrayRandomSongs[:] # wipes array of all contents without making a new one
    self.arrayRandomSongs = self.arraySongs[:]
    random.shuffle(self.arrayRandomSongs)

def chooseListing(self):
    del self.arrayChosen[:] # same here
    for i in xrange(4):
        self.arrayChosen.append(self.arrayRandomSongs[i])
    del self.arrayRandomSongs[0:3]

def chooseTrack(self):
    self.trackChosen = random.choice(self.arrayChosen)

As you can see I would like to select the original index number for the trackChosen object, but since it's randomized I don't see how I could do that.




Aucun commentaire:

Enregistrer un commentaire