我试图将itertools.product()python的功能转换为C代码:
def product(*args, repeat=1):
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
到C代码,但我不理解这个特定的指令:
result = [x+[y] for x in result for y in pool]
任何人都可以为我解释一下吗? 感谢' S
答案 0 :(得分:1)
[ expression for item in list if conditional ]
相当于
for item in list:
if conditional:
expression
列表推导将返回该循环中所有表达式结果的数组。
result = [ x+1 for x in [0,1,2] ]
将依次执行0 + 1,将值保存在数组中,然后对1 + 1和2 + 1执行相同操作。最后结果将是[1,2,3]
答案 1 :(得分:0)
这是一个list comprehension
。它相当于以下内容 -
result = []
for x in result:
for y in pool:
result.append(x+[y])