簡體   English   中英

如何從固定集生成固定大小的所有不同二維數組?

[英]How can I generate all different 2D-arrays of a fixed size from a fixed set?

基本上我想要的是:給定一個 mxn 數組和一個非空集,生成包含這些元素的所有不同列表。 例如,給定一個2x2數組和集合{0,1,2} ,我希望返回:

[[0, 0], [0,0]]

[[0, 0], [0,1]]

[[0, 0], [0,2]]

[[0, 0], [1,0]]

[[0, 0], [1,1]]

[[0, 0], [1,2]]

[[0, 0], [2,0]]

[[0, 0], [2,1]]

[[0, 0], [2,2]]

[[0, 1], [0,0]]

等等。 我在 Python 中工作,但偽代碼或類似的東西會很好。 我似乎無法弄清楚如何做到這一點。

from itertools import product
x = [0, 1, 2] #define the set you want to work on
b = product(x, repeat=4)
# repeat should be the sum of dimension of the desired output 2+2 = 4 in this case
c = [[i[:2], i[2:]] for i in b]
#outputs [[(0, 0), (0, 0)], [(0, 0), (0, 1)], [(0, 0), (0, 2)], [(0, 0), (1, 0)], ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM