1

I was trying to convert the itertools.product() python's fonction to C code:

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)

to C code, but I didn't understand this particular instruction:

result = [x+[y] for x in result for y in pool]

could anyone explain it for me ? thank's

2 Answers 2

1

As Ashish pointed out, it's a list comprehension. In short list comprehensions are basically just a one-liner for a loop with an optional conditional statement (or many conditional statements for that matter) that returns an array.

[ expression for item in list if conditional ]

is equivalent to

for item in list:
if conditional:
    expression

List comprehensions will return an array of all expression results in that loop.

result = [ x+1 for x in [0,1,2] ]

would in turn execute 0+1, save the value in an array, then do the same with 1+1 and 2+1. Finally the result would be [1,2,3]

Sign up to request clarification or add additional context in comments.

Comments

0

It's a list comprehension. It is equivalent to the following -

result = []
for x in result:
    for y in pool:
        result.append(x+[y])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.