Python 具有混合元素的列表的排列列表(np.random.permutation()因ValueError失败)

Python 具有混合元素的列表的排列列表(np.random.permutation()因ValueError失败),python,numpy,permutation,Python,Numpy,Permutation,我试图排列一个由混合类型元素的子列表组成的列表: 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.999

我试图排列一个由混合类型元素的子列表组成的列表:

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

因此,
np.random.shuffle()
解决方案比
np.random.permutation()
快10倍,比
random.shuffle()
快2倍。您需要将列表转换为类型为
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)
那使用什么呢

有关
shuffle
permutation

random之间的区别,请参见。shuffle()会将列表更改到位

修改结构的pythonapi方法通常不返回任何值

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

代码如下所示:

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

如果您只想创建一个
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,并使用以实现相同的效果:

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

您是要生成列表的随机排列,还是要获取所有排列?只是一个随机排列。我将把这些信息添加到问题中。这是一种非常奇怪的使用NumPy的方式。如果你想用NumPy来构建这样的数据结构,你将不得不处理很多尴尬和低效的问题。很可能就是这样。也许只使用下面建议的
random
包更有效。在我的情况下,这会返回
None
?o_O@Gabriel
shuffle
将阵列洗牌到位。打印
b
以查看结果。当然可以。这看起来比Kasramvd的答案更简单。也许我需要做一些计时测试,看看哪一个更有效。正如链接问题中强调的,
permutation
实际上使用了shuffle,正如您从中看到的,因此您可能会发现
shuffle
更有效(或者更可能的是,差异可以忽略不计)
>>> import random
>>> random.shuffle(a)