Shuffling a list of objects

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

    In some cases when using numpy arrays, using random.shuffle created duplicate data in the array.

    An alternative is to use numpy.random.shuffle. If you're working with numpy already, this is the preferred method over the generic random.shuffle.

    numpy.random.shuffle

    Example

    >>> import numpy as np
    >>> import random
    

    Using random.shuffle:

    >>> foo = np.array([[1,2,3],[4,5,6],[7,8,9]])
    >>> foo
    
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    
    
    >>> random.shuffle(foo)
    >>> foo
    
    array([[1, 2, 3],
           [1, 2, 3],
           [4, 5, 6]])
    

    Using numpy.random.shuffle:

    >>> foo = np.array([[1,2,3],[4,5,6],[7,8,9]])
    >>> foo
    
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    
    
    >>> np.random.shuffle(foo)
    >>> foo
    
    array([[1, 2, 3],
           [7, 8, 9],
           [4, 5, 6]])
    

提交回复
热议问题