代码之家  ›  专栏  ›  技术社区  ›  davethegr8 Community Driven Business

使用python洗牌数组,使用python随机化数组项顺序

  •  216
  • davethegr8 Community Driven Business  · 技术社区  · 16 年前

    用python洗牌数组最简单的方法是什么?

    10 回复  |  直到 7 年前
        1
  •  549
  •   David Z    16 年前
    import random
    random.shuffle(array)
    
        2
  •  111
  •   Douglas Leeder    16 年前
    import random
    random.shuffle(array)
    
        3
  •  44
  •   Qy Zuo    7 年前

    sklearn

    from sklearn.utils import shuffle
    X=[1,2,3]
    y = ['one', 'two', 'three']
    X, y = shuffle(X, y, random_state=0)
    print(X)
    print(y)
    

    [2, 1, 3]
    ['two', 'one', 'three']
    

    优点:您可以同时随机选择多个阵列,而不会中断映射。“随机_状态”可以控制重复行为的洗牌。

        4
  •  21
  •   Mark Rhodes    13 年前

    random.shuffle 方法实际上并没有返回任何内容—它只是对给定的列表进行排序。如果您想连锁调用或仅能在一行中声明一个无序数组,您可以执行以下操作:

        import random
        def my_shuffle(array):
            random.shuffle(array)
            return array
    

    然后您可以执行以下操作:

        for suit in my_shuffle(['hearts', 'spades', 'clubs', 'diamonds']):
    
        5
  •  18
  •   Federico klez Culloca    7 年前

    以防您需要一个可以使用的新阵列 sample

    import random
    new_array = random.sample( array, len(array) )
    
        6
  •  12
  •   abcd Luidy    8 年前

    在处理常规Python列表时, random.shuffle() 正如前面的答案所示,我们将完成这项工作。

    但是到了 ndarray numpy.array random.shuffle 似乎打破了原有的 恩达雷 . 以下是一个例子:

    import random
    import numpy as np
    import numpy.random
    
    a = np.array([1,2,3,4,5,6])
    a.shape = (3,2)
    print a
    random.shuffle(a) # a will definitely be destroyed
    print a
    

    np.random.shuffle(a)

    喜欢 随机洗牌 , np.random.shuffle

        7
  •  8
  •   Nhu Trinh    3 年前

    可以使用随机键对数组进行排序

    sorted(array, key = lambda x: random.random())
    

    键只能读取一次,因此排序期间比较项仍然有效。

    但是看起来像 random.shuffle(array)

    这是O(log(N))btw

        8
  •  4
  •   Saber    8 年前

    除了前面的答复外,我还想介绍另一个功能。

    numpy.random.shuffle random.shuffle 执行原地洗牌。但是,如果要返回一个无序数组 numpy.random.permutation 是要使用的函数。

        9
  •  2
  •   Jeeva    8 年前

    我不知道我用过 random.shuffle()

    def shuffle(arr):
        for n in range(len(arr) - 1):
            rnd = random.randint(0, (len(arr) - 1))
            val1 = arr[rnd]
            val2 = arr[rnd - 1]
    
            arr[rnd - 1] = val1
            arr[rnd] = val2
    
        return arr
    
        10
  •  0
  •   MBT Nina Golyandina    6 年前
    # arr = numpy array to shuffle
    
    def shuffle(arr):
        a = numpy.arange(len(arr))
        b = numpy.empty(1)
        for i in range(len(arr)):
            sel = numpy.random.random_integers(0, high=len(a)-1, size=1)
            b = numpy.append(b, a[sel])
            a = numpy.delete(a, sel)
        b = b[1:].astype(int)
        return arr[b]
    
        11
  •  0
  •   Wise Cloud    5 年前

    注意 random.shuffle() 不应在多维数组上使用,因为它会导致重复。

    import numpy as np
    x = np.zeros((10, 2, 3))
    
    for i in range(10):
       x[i, ...] = i*np.ones((2,3))
    

    因此,沿着第一个轴,第i个元素对应于2x3矩阵,其中所有元素都等于i。

    np.random.shuffle(x) ,数组将根据需要沿第一个轴洗牌。然而,使用 random.shuffle(x) len(np.unique(x)) 洗牌后,你会得到10(如预期)的 np.random.shuffle() .