Shuffling a list of objects

后端 未结 23 1705
眼角桃花
眼角桃花 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:59

    def shuffle(_list):
        if not _list == []:
            import random
            list2 = []
            while _list != []:
                card = random.choice(_list)
                _list.remove(card)
                list2.append(card)
            while list2 != []:
                card1 = list2[0]
                list2.remove(card1)
                _list.append(card1)
            return _list
    

提交回复
热议问题