简体   繁体   中英

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

Essentially what I want is: given an mxn array, and a non-empty set, to generate all different lists containing those elements. For example, given a 2x2 array, and the set {0,1,2} , I'd expect back:

[[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]]

and so on. I'm working in Python, but pseudo-code or anything like that would be fine. I just can't seem to figure out how to do this.

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)], ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM