itertools产品功能背后的概念 [英] The Concept Behind itertools's product Function
本文介绍了itertools产品功能背后的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以基本上我想了解itertools中product()函数的概念.我的意思是收益率和收益率有什么区别.而且该代码可以缩短吗?
so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway.
def product1(*args, **kwds):
pools = map(tuple, args) * kwds.get('repeat', 1)
n = len(pools)
if n == 0:
yield ()
return
if any(len(pool) == 0 for pool in pools):
return
indices = [0] * n
yield tuple(pool[i] for pool, i in zip(pools, indices))
while 1:
for i in reversed(range(n)): # right to left
if indices[i] == len(pools[i]) - 1:
continue
indices[i] += 1
for j in range(i+1, n):
indices[j] = 0
yield tuple(pool[i] for pool, i in zip(pools, indices))
break
else:
return
推荐答案
此代码应能完成工作:
bytes = [i for i in range(2**(n))]
AB= []
for obj in bytes:
t = str(bin(obj))[2:]
t= '0'*(n-len(t)) + t
AB.append(t.replace('0','A').replace('1','B'))
n是想要的字符串大小
n being the string size wanted
这篇关于itertools产品功能背后的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文