PYTHON ITERTOOLS
Posted myrj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于PYTHON ITERTOOLS的主要内容,如果未能解决你的问题,请参考以下文章
这段代码很Pythonic | 相见恨晚的 itertools 库