具有混合元素的列表的置换列表(np.random.permutation()失败,带有ValueError)

时间:2016-09-08 19:34:35

标签: python numpy permutation

我试图置换由具有混合类型元素的子列表组成的列表:

Counter

这将失败:

isanagram

是否有内置函数可以让我生成这样的排列?

我需要生成一个随机排列,我没有尝试获得所有可能的排列。

我检查了给出的三个答案:

import numpy as np

a0 = ['122', 877.503017, 955.471176, [21.701201, 1.315585]]
a1 = ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]]
a2 = ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]]
a3 = ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]]

a = [a0, a1, a2, a3]

b = np.random.permutation(a)

结果是:

ValueError: cannot set an array element with a sequence

因此import time import random # np.random.permutation() start = time.time() for _ in np.arange(100000): b = np.random.permutation([np.array(i, dtype='object') for i in a]) print(time.time() - start) # np.random.shuffle() start = time.time() for _ in np.arange(100000): b = a[:] np.random.shuffle(b) print(time.time() - start) # random.shuffle() start = time.time() for _ in np.arange(100000): random.shuffle(a) print(time.time() - start) 解决方案比1.47580695152 0.11471414566 0.26300907135 快约10倍,比np.random.shuffle()快2倍。

4 个答案:

答案 0 :(得分:2)

如何使用np.random.shuffle

# if you want the result in another list, otherwise just apply shuffle to a
b = a[:]
# shuffle the elements
np.random.shuffle(b)
# see the result of the shuffling
print(b)

有关shufflepermutation

之间的区别,请参阅this answer

答案 1 :(得分:2)

如果你只是想创建一个a = [a0, a1, a2, a3]的随机排列,我可以建议换一下索引吗?

>>> random_indices = np.random.permutation(np.arange(len(a)))
>>> a_perm = [a[i] for i in random_indices]
... # Or just use the indices as you see fit...

如果您正在使用numpy ,请完全跳过numpy,然后使用random.shuffle来实现相同的效果:

>>> import random
>>> random.shuffle(a)

答案 2 :(得分:1)

您需要将列表转换为类型为object()的numpy数组,以便random.permutation()可以将列表解释为numpy类型而不是序列:

>>> a = [np.array(i, dtype='object') for i in a]
>>> 
>>> np.random.permutation(a)
array([['122', 877.503017, 955.471176, [21.701201, 1.315585]],
       ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]],
       ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]],
       ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]]], dtype=object)

您还可以使用numpy.array()从列表中创建一个uniqe数组,而不是使用列表解析:

>>> a = np.array((a0, a1, a2, a3), dtype='object')
>>> a
array([['122', 877.503017, 955.471176, [21.701201, 1.315585]],
       ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]],
       ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]],
       ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]]], dtype=object)
>>> np.random.permutation(a)
array([['122', 877.503017, 955.471176, [21.701201, 1.315585]],
       ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]],
       ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]],
       ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]]], dtype=object)
>>> np.random.permutation(a)
array([['177', 1038.686843, 1018.987868, [19.539959, 1.183997]],
       ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]],
       ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]],
       ['122', 877.503017, 955.471176, [21.701201, 1.315585]]], dtype=object)

答案 3 :(得分:0)

random.shuffle()更改列表。

就地改变结构的Python API方法通常会返回None。

请尝试random.sample(a,len(a))

代码如下:

a = a[:]
b = random.sample(a,len(a))