在Python中对所有嵌套列表(列表列表)中的所有元素进行无序排列/排列组合? [英] Stream all unordered/shuffled unique permutation of elements in a nested list (list of list) in Python?

查看:1133
本文介绍了在Python中对所有嵌套列表(列表列表)中的所有元素进行无序排列/排列组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想生成这个嵌套列表的所有8个唯一排列,但是我的应用程序绝对需要输出为(伪 - )随机化和无序的。通常,排列策略会按顺序产生排列,但我希望能够按顺序排列所有排列。

此外,这个必须通过某个生成器来完成,因为嵌套列表可能非常长,并且唯一排列的数目可以组合爆炸。



例如,上面的列表需要以下输出:

 ( (1,2,3,5,6,7,8,9,10,12)
(1,2,3,5,6,7,8,9,10,12)
(1,2,3,5,6,7,8,9,10,12) (1,2,3,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,10,12) 0,2,4,5,6,7,8,9,10,12)
(1, (1,2,3,4,5,6,7,8,9,11,12)
(1,2,4,5,6,7,8,9,11,12)
(0,2,3,5,6,7,8,9,11,12)
(1,2,4,5,6,7,8,9,11,12) 2,4,5,6,7,8,9,11,12)

...这与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)

甚至有一些解决方案可以完全实现itertools.product的功能,但会产生乱序排列饶了我很多。任何帮助表示赞赏。



以下代码说明了我现有的方法。

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


解决方案

您可以尝试迭代以下生成器: / 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.

这篇关于在Python中对所有嵌套列表(列表列表)中的所有元素进行无序排列/排列组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆