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)
==================== 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

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))  #形成可重复的组合。从0,1,2,3中得到可重复的2个字符,所有组合形成LIST如:[('1','2'),('1','0')....]
print(mylist)
#print(len(mylist))
swd=("".join(x) for x in itertools.product("0123",repeat=2))  #得出具体一组如('1','2'),x代表1,2循环表示出来,用join连接起来形成“12”。有许多组形成列表
while True:
    try:
        aa=next(swd) #取得swd一组,反复取出,至最后一组
        print(aa)
    except StopIteration as e:#取得最后一个,再取下一个,报这个错,捕获,而终止
        break
        

 

相关文章:

  • 2022-01-30
  • 2021-09-17
  • 2022-01-23
  • 2021-09-12
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-30
  • 2021-07-17
  • 2021-08-14
  • 2021-06-27
  • 2021-06-10
  • 2022-12-23
相关资源
相似解决方案