python内置函数permutations
what
permutations是python的itertools模块的一个函数,可以用来求可迭代对象的全排列
itertools.permutations(iterable, r)
- iterable:可迭代对象
- r:全排列的长度
- return:tuple
后面r的作用解释一下,加入iterable中元素有n个,r=m,就相当于先在n中选m个,再对m个进行全排列
也就是\(C^m_n\)
结果可见下面这段代码\(C^3_5=60\)所有list的长度为60
import itertools
nums = ["1", "2", "3", "4", "5"]
res = itertools.permutations(nums, 3)
res = list(res)
print(len(res))
for num in res:
print(num, type(num))
how
import itertools
nums = ["1", "2", "3", "4", "5"]
res = itertools.permutations(nums)
print(res)
for num in res:
print(num, type(num))
还需要解释的一点是iterable允许有重复的元素,他们会被按下标认为是不同的