PYTHON ITERTOOLS

import itertools

mylist=list(itertools.permutations([1,2,3,4],3)) #排列
print(mylist)
print(len(mylist))
mylist=list(itertools.combinations([1,2,3,4],3))  #组合
print(mylist)
print(len(mylist))

结果:

==================== RESTART: D:/Python/Python37/mima1.py ====================
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
24
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
4
>>>

import itertools

mylist=list(itertools.permutations([1,2,3,4],3)) #排列
#print(mylist)
#print(len(mylist))
mylist=list(itertools.combinations([1,2,3,4],3))  #组合
#print(mylist)
#print(len(mylist))
mylist=list(itertools.product("0123",repeat=2))  #形成可重复的组合
#print(mylist)
#print(len(mylist))
swd=("".join(x) for x in itertools.product("0123",repeat=2))
while True:
    try:
        aa=next(swd)
        print(aa)
    except StopIteration as e:
        break