且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中对所有嵌套列表(列表列表)中的所有元素进行无序排列/排列组合?

更新时间:2022-10-17 19:51:36

您可以尝试迭代以下生成器: / p>

  def random_perm(l):
,而True:
yield [random.choice(sublist) in b]

示例用法:

  l = [[0,1],[2],[3,4],[5,5],[6 
g = random_perm(l)
代表范围内的_(10):
print (下一个(g))

输出:

  [0,2,4,5,6,7,8,9,10,12] 
[1,2,4,5,6,7,8 ,9,11,12]
[0,2,3,5,6,7,8,9,10,12]
[0,2,3,5,6,7,8 ,9,10,12]
[0,2,3,5,6,7,8,9,11,12]
[1,2,4,5,6,7,8 ,9,10,12]
[0,2,3,5,6,7,8,9,11,12]
[1,2,4,5,6,7,8 ,9,11,12]
[1,2,4,5,6,7,8,9,10,12]
[0,2,4,5,6,7,8 ,9,10,12]

然而,正如其他人在评论中指出的,除非你缓存在某种程度上,产量在记忆中产生,你不能保证你不会得到重复。您也无法保证在任何8次连续迭代中获得全部8个独特迭代。


I have two-level nested list like following.

[[0, 1], [2], [3, 4], [5, 5], [6], [7], [8], [9], [10, 11], [12]]

I want to generate all 8 unique permutations of this nested list, but my application absolutely needs the output to be (pseudo-)randomized and unordered. Usually, permutation strategies produce the permutations in order, but I want to be able to produce all permutations out of order.

Moreover, this MUST be done through some generator, as the nested list can be very long, and number of unique permutations can explode combinatorially.

Case in point, the following output is desired for the above list.

(0, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(0, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 10, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 11, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 11, 12)

...as opposed to the following, which is generated by itertools.product(*some_list):

(0, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(0, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(0, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(0, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 11, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 10, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 11, 12)
(0, 2, 4, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 3, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 10, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 11, 12)
(1, 2, 4, 5, 6, 7, 8, 9, 10, 12)

Even some solution that does exactly what itertools.product does, but generates permutations out of order will help me a lot. Any help is appreciated.

The following code illustrates my existing approach.

def perm_attempt():
    meta_seq = [[0, 1], [2], [3, 4], [5, 5], [6], [7], [8], [9], [10, 11], [12]]
    print meta_seq
    iter_count = np.prod([len(set(x)) for x in meta_seq])
    print iter_count
    print
    set_l = set()
    for _ in xrange(iter_count*10):
        l = [np.random.choice(x) for x in meta_seq]
        # print l
        set_l.add(tuple(l))
    print
    print len(set_l)
    print
    # for s in set_l:
    #     print s

You can try iterating over the following generator:

def random_perm(l):
    while True:
        yield [random.choice(sublist) for sublist in l]

Sample usage:

l = [[0, 1], [2], [3, 4], [5, 5], [6], [7], [8], [9], [10, 11], [12]]
g = random_perm(l)
for _ in range(10):
    print(next(g))

Output:

[0, 2, 4, 5, 6, 7, 8, 9, 10, 12]
[1, 2, 4, 5, 6, 7, 8, 9, 11, 12]
[0, 2, 3, 5, 6, 7, 8, 9, 10, 12]
[0, 2, 3, 5, 6, 7, 8, 9, 10, 12]
[0, 2, 3, 5, 6, 7, 8, 9, 11, 12]
[1, 2, 4, 5, 6, 7, 8, 9, 10, 12]
[0, 2, 3, 5, 6, 7, 8, 9, 11, 12]
[1, 2, 4, 5, 6, 7, 8, 9, 11, 12]
[1, 2, 4, 5, 6, 7, 8, 9, 10, 12]
[0, 2, 4, 5, 6, 7, 8, 9, 10, 12]

However, as others have pointed out in the comments, unless you cache the yield results in memory somehow, you can't really guarantee you won't get duplicates. You are also not guaranteed to get all 8 unique iterations in any 8 consecutive iterations.