Shuffling a list of objects

后端 未结 23 2054
眼角桃花
眼角桃花 2020-11-22 00:29

I have a list of objects and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is the

23条回答
  •  眼角桃花
    2020-11-22 00:49

    """ to shuffle random, set random= True """
    
    def shuffle(x,random=False):
         shuffled = []
         ma = x
         if random == True:
             rando = [ma[i] for i in np.random.randint(0,len(ma),len(ma))]
             return rando
         if random == False:
              for i in range(len(ma)):
              ave = len(ma)//3
              if i < ave:
                 shuffled.append(ma[i+ave])
              else:
                 shuffled.append(ma[i-ave])    
         return shuffled
    

提交回复
热议问题