range()以及按需生成项目的返回生成器。您无需调用并将它们转换为列表。只需直接迭代它们并逐个访问项目即可。permutation()list()num = 11base = range(1, num+1)permutations = itertools.permutations(base)for permutation in permutations: # Do something with `permutation`.(请注意,一个生成器只能使用一次。如果要多次迭代排列,则需要多次调用。itertools.permutations()要在 n 个项目后停止,请使用 :itertools.islice()for permutation in itertools.islice(permutations, n): # Do something with `permutation`.您也可以在开始时跳过项目。这将跳过前五个排列:for permutation in itertools.islice(permutations, 5, n): # Do something with `permutation`.如果要计算排列,可以添加 ,它将索引附加到每个条目:enumerate()for i, permutation in enumerate(itertools.islice(permutations, n)): # Skip the fifth permutation. if i == 4: continue # Do something with `permutation`.顺便说一句,请使用小写字母作为变量名称。只有类名应大写。