Guess language of text using ZIP

列出所有列表的所有组合(list of all combination from multiple lists)

dcy posted @ 2010年5月04日 10:20 in Python with tags python Algorithm , 3122 阅读
[1,2],[3,4,5],[6],[7,8,9,10]--》[1, 3, 6, 7], [2, 3, 6, 7], [1, 4, 6, 7], [2, 4, 6, 7], [1, 5, 6, 7], [2, 5, 6, 7], [1, 3, 6, 8], [2, 3, 6, 8], [1, 4, 6, 8], [2, 4, 6, 8], [1, 5, 6, 8], [2, 5, 6, 8], [1, 3, 6, 9], [2, 3, 6, 9], [1, 4, 6, 9], [2, 4, 6, 9], [1, 5, 6, 9], [2, 5, 6, 9], [1, 3, 6, 10], [2, 3, 6, 10], [1, 4, 6, 10], [2, 4, 6, 10], [1, 5, 6, 10], [2, 5, 6, 10]
a = [[1,2],[3,4,5],[6],[7,8,9,10]]

r=[[]]
for x in a:
    t = []
        for y in x:
            for i in r:
                t.append(i+[y])
    r = t

print r

 

 

#!/usr/bin/env python

a = [[1, 2], [3, 4, 5], [6], [7, 8, 9, 10]]
r = [[]]
for x in a:
    r = [i + [y] for y in x for i in r]
print r

 

参考于acivestate

 

 

Alyssa 说:
2022年12月21日 21:21

There are a few different ways to list all combinations of all lists. One way is to use the itertools module. The itertools module has a product function that can be used to list all cbd oil combinations of all lists. Another way is to use the moreitertools module. The moreitertools module has a combinations_with_replacement function that can be used to list all combinations of all lists.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter