我试图按照以下代码访问由生成器创建的所有元素:
from itertools import combinations
Fatores = ['Ft', 'Fp', 'Floc']
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)
panel = combinations(Fatores,2)
for subset in panel:
print(subset)
但是,我收到以下错误消息:
TypeError:“范围”对象不支持项目分配
有人有解决这个问题的提示吗?
亲切问候
答案 0 :(得分:0)
您的代码有问题
1。
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
索引分配应高于返回值,然后返回应仅返回变量
我不太确定这部分要做什么,但是我认为上面应该是for循环