我是Python的新手。我有一个Python 2D数组[[a,b,c],[d,e,f],[g,h,j]]
,并且想改组3个内部列表,而不是目录。我似乎无法访问numpy,但会认为这很简单。随机播放似乎不适用于二维数组,因为它不返回任何内容。请帮助!!
例如,我想要类似[[d,e,f],[a,b,c],[g,h,j]]
的东西...
答案 0 :(得分:0)
您可以使用shuffle
中的功能random
:
import random
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
random.shuffle(arr)
示例输出:
>>> arr
[['g', 'h', 'j'], ['d', 'e', 'f'], ['a', 'b', 'c']]
或者,您可以对numpy.random.shuffle
做同样的事情:
import numpy as np
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
np.random.shuffle(arr)
>>> arr
[['g', 'h', 'j'], ['a', 'b', 'c'], ['d', 'e', 'f']]