我想使用 itertools 从字母表中的每个字母中获取所有可能的 3 个字母排列。这返回空白:
import itertools
def permutations(ABCDEFGHIJKLMNOPQRSTUVWXYZ, r=3):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
我做错了什么?
请您参考如下方法:
你有点糊涂了,那只是解释permutations 作用的代码。 itertools 实际上是用 C 代码编写的,python equivalent 只是为了展示它是如何工作的。
>>> from itertools import permutations
>>> from string import ascii_uppercase
>>> for x in permutations(ascii_uppercase, r=3):
print x
('A', 'B', 'C')
('A', 'B', 'D')
('A', 'B', 'E')
('A', 'B', 'F')
.....
应该没问题
