迭代元组中列表中的成员

时间:2013-10-02 15:43:56

标签: list loops python-3.x tuples permutation

想象一下这个列表

[[1,2,3],[1,2,3],[1,2,3]]

如何获得这些排列:

[[1,2,3],[1,2,3],[1,2,3]]
[[1,2,3],[1,2,3],[1,3,2]]
[[1,2,3],[1,2,3],[2,1,3]]
[[1,2,3],[1,2,3],[2,3,1]]
....
[[1,2,3],[1,3,2],[1,2,3]]
[[1,2,3],[1,3,2],[1,3,2]]
[[1,2,3],[1,3,2],[2,1,3]]
....
[[3,2,1],[3,2,1],[3,2,1]]

1 个答案:

答案 0 :(得分:0)

import itertools
seq = [[1,2,3], [1,2,3], [1,2,3]]
for item in itertools.product(*[itertools.permutations(sublist) for sublist in seq]):
    print item

结果:

((1, 2, 3), (1, 2, 3), (1, 2, 3))
((1, 2, 3), (1, 2, 3), (1, 3, 2))
((1, 2, 3), (1, 2, 3), (2, 1, 3))
((1, 2, 3), (1, 2, 3), (2, 3, 1))
((1, 2, 3), (1, 2, 3), (3, 1, 2))
((1, 2, 3), (1, 2, 3), (3, 2, 1))
((1, 2, 3), (1, 3, 2), (1, 2, 3))
((1, 2, 3), (1, 3, 2), (1, 3, 2))
((1, 2, 3), (1, 3, 2), (2, 1, 3))
((1, 2, 3), (1, 3, 2), (2, 3, 1))
((1, 2, 3), (1, 3, 2), (3, 1, 2))
((1, 2, 3), (1, 3, 2), (3, 2, 1))
((1, 2, 3), (2, 1, 3), (1, 2, 3))
((1, 2, 3), (2, 1, 3), (1, 3, 2))
((1, 2, 3), (2, 1, 3), (2, 1, 3))
((1, 2, 3), (2, 1, 3), (2, 3, 1))
((1, 2, 3), (2, 1, 3), (3, 1, 2))
((1, 2, 3), (2, 1, 3), (3, 2, 1))
((1, 2, 3), (2, 3, 1), (1, 2, 3))
((1, 2, 3), (2, 3, 1), (1, 3, 2))
((1, 2, 3), (2, 3, 1), (2, 1, 3))
((1, 2, 3), (2, 3, 1), (2, 3, 1))
((1, 2, 3), (2, 3, 1), (3, 1, 2))
((1, 2, 3), (2, 3, 1), (3, 2, 1))
((1, 2, 3), (3, 1, 2), (1, 2, 3))
((1, 2, 3), (3, 1, 2), (1, 3, 2))
((1, 2, 3), (3, 1, 2), (2, 1, 3))
((1, 2, 3), (3, 1, 2), (2, 3, 1))
((1, 2, 3), (3, 1, 2), (3, 1, 2))
((1, 2, 3), (3, 1, 2), (3, 2, 1))
((1, 2, 3), (3, 2, 1), (1, 2, 3))
((1, 2, 3), (3, 2, 1), (1, 3, 2))
((1, 2, 3), (3, 2, 1), (2, 1, 3))
((1, 2, 3), (3, 2, 1), (2, 3, 1))
((1, 2, 3), (3, 2, 1), (3, 1, 2))
((1, 2, 3), (3, 2, 1), (3, 2, 1))
((1, 3, 2), (1, 2, 3), (1, 2, 3))
((1, 3, 2), (1, 2, 3), (1, 3, 2))
((1, 3, 2), (1, 2, 3), (2, 1, 3))
((1, 3, 2), (1, 2, 3), (2, 3, 1))
((1, 3, 2), (1, 2, 3), (3, 1, 2))
((1, 3, 2), (1, 2, 3), (3, 2, 1))
((1, 3, 2), (1, 3, 2), (1, 2, 3))
((1, 3, 2), (1, 3, 2), (1, 3, 2))
((1, 3, 2), (1, 3, 2), (2, 1, 3))
((1, 3, 2), (1, 3, 2), (2, 3, 1))
((1, 3, 2), (1, 3, 2), (3, 1, 2))
((1, 3, 2), (1, 3, 2), (3, 2, 1))
((1, 3, 2), (2, 1, 3), (1, 2, 3))
((1, 3, 2), (2, 1, 3), (1, 3, 2))
((1, 3, 2), (2, 1, 3), (2, 1, 3))
((1, 3, 2), (2, 1, 3), (2, 3, 1))
((1, 3, 2), (2, 1, 3), (3, 1, 2))
((1, 3, 2), (2, 1, 3), (3, 2, 1))
((1, 3, 2), (2, 3, 1), (1, 2, 3))
((1, 3, 2), (2, 3, 1), (1, 3, 2))
((1, 3, 2), (2, 3, 1), (2, 1, 3))
((1, 3, 2), (2, 3, 1), (2, 3, 1))
((1, 3, 2), (2, 3, 1), (3, 1, 2))
((1, 3, 2), (2, 3, 1), (3, 2, 1))
((1, 3, 2), (3, 1, 2), (1, 2, 3))
((1, 3, 2), (3, 1, 2), (1, 3, 2))
((1, 3, 2), (3, 1, 2), (2, 1, 3))
((1, 3, 2), (3, 1, 2), (2, 3, 1))
((1, 3, 2), (3, 1, 2), (3, 1, 2))
((1, 3, 2), (3, 1, 2), (3, 2, 1))
((1, 3, 2), (3, 2, 1), (1, 2, 3))
((1, 3, 2), (3, 2, 1), (1, 3, 2))
((1, 3, 2), (3, 2, 1), (2, 1, 3))
((1, 3, 2), (3, 2, 1), (2, 3, 1))
((1, 3, 2), (3, 2, 1), (3, 1, 2))
((1, 3, 2), (3, 2, 1), (3, 2, 1))
((2, 1, 3), (1, 2, 3), (1, 2, 3))
((2, 1, 3), (1, 2, 3), (1, 3, 2))
((2, 1, 3), (1, 2, 3), (2, 1, 3))
((2, 1, 3), (1, 2, 3), (2, 3, 1))
((2, 1, 3), (1, 2, 3), (3, 1, 2))
((2, 1, 3), (1, 2, 3), (3, 2, 1))
((2, 1, 3), (1, 3, 2), (1, 2, 3))
((2, 1, 3), (1, 3, 2), (1, 3, 2))
((2, 1, 3), (1, 3, 2), (2, 1, 3))
((2, 1, 3), (1, 3, 2), (2, 3, 1))
((2, 1, 3), (1, 3, 2), (3, 1, 2))
((2, 1, 3), (1, 3, 2), (3, 2, 1))
((2, 1, 3), (2, 1, 3), (1, 2, 3))
((2, 1, 3), (2, 1, 3), (1, 3, 2))
((2, 1, 3), (2, 1, 3), (2, 1, 3))
((2, 1, 3), (2, 1, 3), (2, 3, 1))
((2, 1, 3), (2, 1, 3), (3, 1, 2))
((2, 1, 3), (2, 1, 3), (3, 2, 1))
((2, 1, 3), (2, 3, 1), (1, 2, 3))
((2, 1, 3), (2, 3, 1), (1, 3, 2))
((2, 1, 3), (2, 3, 1), (2, 1, 3))
((2, 1, 3), (2, 3, 1), (2, 3, 1))
((2, 1, 3), (2, 3, 1), (3, 1, 2))
((2, 1, 3), (2, 3, 1), (3, 2, 1))
((2, 1, 3), (3, 1, 2), (1, 2, 3))
((2, 1, 3), (3, 1, 2), (1, 3, 2))
((2, 1, 3), (3, 1, 2), (2, 1, 3))
((2, 1, 3), (3, 1, 2), (2, 3, 1))
((2, 1, 3), (3, 1, 2), (3, 1, 2))
((2, 1, 3), (3, 1, 2), (3, 2, 1))
((2, 1, 3), (3, 2, 1), (1, 2, 3))
((2, 1, 3), (3, 2, 1), (1, 3, 2))
((2, 1, 3), (3, 2, 1), (2, 1, 3))
((2, 1, 3), (3, 2, 1), (2, 3, 1))
((2, 1, 3), (3, 2, 1), (3, 1, 2))
((2, 1, 3), (3, 2, 1), (3, 2, 1))
((2, 3, 1), (1, 2, 3), (1, 2, 3))
((2, 3, 1), (1, 2, 3), (1, 3, 2))
((2, 3, 1), (1, 2, 3), (2, 1, 3))
((2, 3, 1), (1, 2, 3), (2, 3, 1))
((2, 3, 1), (1, 2, 3), (3, 1, 2))
((2, 3, 1), (1, 2, 3), (3, 2, 1))
((2, 3, 1), (1, 3, 2), (1, 2, 3))
((2, 3, 1), (1, 3, 2), (1, 3, 2))
((2, 3, 1), (1, 3, 2), (2, 1, 3))
((2, 3, 1), (1, 3, 2), (2, 3, 1))
((2, 3, 1), (1, 3, 2), (3, 1, 2))
((2, 3, 1), (1, 3, 2), (3, 2, 1))
((2, 3, 1), (2, 1, 3), (1, 2, 3))
((2, 3, 1), (2, 1, 3), (1, 3, 2))
((2, 3, 1), (2, 1, 3), (2, 1, 3))
((2, 3, 1), (2, 1, 3), (2, 3, 1))
((2, 3, 1), (2, 1, 3), (3, 1, 2))
((2, 3, 1), (2, 1, 3), (3, 2, 1))
((2, 3, 1), (2, 3, 1), (1, 2, 3))
((2, 3, 1), (2, 3, 1), (1, 3, 2))
((2, 3, 1), (2, 3, 1), (2, 1, 3))
((2, 3, 1), (2, 3, 1), (2, 3, 1))
((2, 3, 1), (2, 3, 1), (3, 1, 2))
((2, 3, 1), (2, 3, 1), (3, 2, 1))
((2, 3, 1), (3, 1, 2), (1, 2, 3))
((2, 3, 1), (3, 1, 2), (1, 3, 2))
((2, 3, 1), (3, 1, 2), (2, 1, 3))
((2, 3, 1), (3, 1, 2), (2, 3, 1))
((2, 3, 1), (3, 1, 2), (3, 1, 2))
((2, 3, 1), (3, 1, 2), (3, 2, 1))
((2, 3, 1), (3, 2, 1), (1, 2, 3))
((2, 3, 1), (3, 2, 1), (1, 3, 2))
((2, 3, 1), (3, 2, 1), (2, 1, 3))
((2, 3, 1), (3, 2, 1), (2, 3, 1))
((2, 3, 1), (3, 2, 1), (3, 1, 2))
((2, 3, 1), (3, 2, 1), (3, 2, 1))
((3, 1, 2), (1, 2, 3), (1, 2, 3))
((3, 1, 2), (1, 2, 3), (1, 3, 2))
((3, 1, 2), (1, 2, 3), (2, 1, 3))
((3, 1, 2), (1, 2, 3), (2, 3, 1))
((3, 1, 2), (1, 2, 3), (3, 1, 2))
((3, 1, 2), (1, 2, 3), (3, 2, 1))
((3, 1, 2), (1, 3, 2), (1, 2, 3))
((3, 1, 2), (1, 3, 2), (1, 3, 2))
((3, 1, 2), (1, 3, 2), (2, 1, 3))
((3, 1, 2), (1, 3, 2), (2, 3, 1))
((3, 1, 2), (1, 3, 2), (3, 1, 2))
((3, 1, 2), (1, 3, 2), (3, 2, 1))
((3, 1, 2), (2, 1, 3), (1, 2, 3))
((3, 1, 2), (2, 1, 3), (1, 3, 2))
((3, 1, 2), (2, 1, 3), (2, 1, 3))
((3, 1, 2), (2, 1, 3), (2, 3, 1))
((3, 1, 2), (2, 1, 3), (3, 1, 2))
((3, 1, 2), (2, 1, 3), (3, 2, 1))
((3, 1, 2), (2, 3, 1), (1, 2, 3))
((3, 1, 2), (2, 3, 1), (1, 3, 2))
((3, 1, 2), (2, 3, 1), (2, 1, 3))
((3, 1, 2), (2, 3, 1), (2, 3, 1))
((3, 1, 2), (2, 3, 1), (3, 1, 2))
((3, 1, 2), (2, 3, 1), (3, 2, 1))
((3, 1, 2), (3, 1, 2), (1, 2, 3))
((3, 1, 2), (3, 1, 2), (1, 3, 2))
((3, 1, 2), (3, 1, 2), (2, 1, 3))
((3, 1, 2), (3, 1, 2), (2, 3, 1))
((3, 1, 2), (3, 1, 2), (3, 1, 2))
((3, 1, 2), (3, 1, 2), (3, 2, 1))
((3, 1, 2), (3, 2, 1), (1, 2, 3))
((3, 1, 2), (3, 2, 1), (1, 3, 2))
((3, 1, 2), (3, 2, 1), (2, 1, 3))
((3, 1, 2), (3, 2, 1), (2, 3, 1))
((3, 1, 2), (3, 2, 1), (3, 1, 2))
((3, 1, 2), (3, 2, 1), (3, 2, 1))
((3, 2, 1), (1, 2, 3), (1, 2, 3))
((3, 2, 1), (1, 2, 3), (1, 3, 2))
((3, 2, 1), (1, 2, 3), (2, 1, 3))
((3, 2, 1), (1, 2, 3), (2, 3, 1))
((3, 2, 1), (1, 2, 3), (3, 1, 2))
((3, 2, 1), (1, 2, 3), (3, 2, 1))
((3, 2, 1), (1, 3, 2), (1, 2, 3))
((3, 2, 1), (1, 3, 2), (1, 3, 2))
((3, 2, 1), (1, 3, 2), (2, 1, 3))
((3, 2, 1), (1, 3, 2), (2, 3, 1))
((3, 2, 1), (1, 3, 2), (3, 1, 2))
((3, 2, 1), (1, 3, 2), (3, 2, 1))
((3, 2, 1), (2, 1, 3), (1, 2, 3))
((3, 2, 1), (2, 1, 3), (1, 3, 2))
((3, 2, 1), (2, 1, 3), (2, 1, 3))
((3, 2, 1), (2, 1, 3), (2, 3, 1))
((3, 2, 1), (2, 1, 3), (3, 1, 2))
((3, 2, 1), (2, 1, 3), (3, 2, 1))
((3, 2, 1), (2, 3, 1), (1, 2, 3))
((3, 2, 1), (2, 3, 1), (1, 3, 2))
((3, 2, 1), (2, 3, 1), (2, 1, 3))
((3, 2, 1), (2, 3, 1), (2, 3, 1))
((3, 2, 1), (2, 3, 1), (3, 1, 2))
((3, 2, 1), (2, 3, 1), (3, 2, 1))
((3, 2, 1), (3, 1, 2), (1, 2, 3))
((3, 2, 1), (3, 1, 2), (1, 3, 2))
((3, 2, 1), (3, 1, 2), (2, 1, 3))
((3, 2, 1), (3, 1, 2), (2, 3, 1))
((3, 2, 1), (3, 1, 2), (3, 1, 2))
((3, 2, 1), (3, 1, 2), (3, 2, 1))
((3, 2, 1), (3, 2, 1), (1, 2, 3))
((3, 2, 1), (3, 2, 1), (1, 3, 2))
((3, 2, 1), (3, 2, 1), (2, 1, 3))
((3, 2, 1), (3, 2, 1), (2, 3, 1))
((3, 2, 1), (3, 2, 1), (3, 1, 2))
((3, 2, 1), (3, 2, 1), (3, 2, 1))