【发布时间】:2017-03-16 18:01:42
【问题描述】:
试图让我在一个数组中随机化 4 个对象并随机选择其中一个对象来工作。我需要能够取回所选对象的原始索引号。关于我应该如何尽可能短地写这个的任何想法?
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)
如您所见,我想为 trackChosen 对象选择 arayChosen 索引号,但由于它是随机的,我不知道该怎么做。
【问题讨论】:
-
我的意思是我想获取arrayChosen的索引号,而不是原始的原始数组
标签: python arrays python-2.7 random