Shuffling a list of objects

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

    You can go for this:

    >>> A = ['r','a','n','d','o','m']
    >>> B = [1,2,3,4,5,6]
    >>> import random
    >>> random.sample(A+B, len(A+B))
    [3, 'r', 4, 'n', 6, 5, 'm', 2, 1, 'a', 'o', 'd']
    

    if you want to go back to two lists, you then split this long list into two.

提交回复
热议问题