在哪里可以找到 itertools.combinations() 函数的源代码 [英] Where can I find source code for itertools.combinations() function
本文介绍了在哪里可以找到 itertools.combinations() 函数的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种编写组合函数的方法.我在哪里可以找到它?
I'm trying to find a way to write a combination function. Where can I find it?
推荐答案
见itertools.combinations.这个函数有一个等效的代码:
See in the documentation of itertools.combinations. There is an equivalent code for this function:
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
这篇关于在哪里可以找到 itertools.combinations() 函数的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文