+ 4
itertools: product
I made it pass the question in python lesson on itertools but while learning but i am curious to how it reached the conclusion of “6”, I don’t want to continue the lesson until is grasp this. from itertools import product a={1, 2} print(len(list(product(range(3), a))) #what does list mean in this line? It is said the ‘product’ is used to make possible combinations but how did reach 6? Can anyone shine some light on this question? Thanks
18 ответов
+ 8
Click run and see combinations, len just counting them.
https://code.sololearn.com/cANRNjct1Nz7/?ref=app
+ 5
You just create list [a, b, c, ...] with all combinations of {1,2} and [0,1,2].
[0,1,2] is result of range(3).
+ 5
Try to remember how we created quadratic equations by manually squaring x+y to get x^2+xy+yx+y^2.
its simply x(x+y)+y(x+y)
in our case we have set a as {1,2} and another iterable created by range(3) which is [0,1,2]
the product ideally becomes 1×(0,1,2) + 2(0,1,2). so you get (1,0),(1,1),(1,2) and (2,0),(2,1),(2,2). and those are six tuples
+ 5
6
+ 2
Alrighty, so the code is building a list of the different combinations of "a" (1,2) the range is 3 which will give you the following list (0,1)(1,1)(2,1)(0,2)(1,2)(2,2).
If you run print(list(product(range(3), a))) #that's without "len" it will print out that list. So if you count the number of listed items the answer is 6.
+ 1
Hubert Dudek ,Thank you! you beautiful person
I didn't take the len into account when thinking this... thank you again
+ 1
Thanks to Babeetha
0
[0,2]wha??
I get what you were trying to say about the combinations but...
the output is 6... how excatly did it reach it?
0
What is the output of this code?
from itertools import product
a={1, 2}
print(len(list(product(range(3), a))))
output:6
0
The ans is 6
0
What is the output of this code?
from itertools import product
a={1, 2}
print(len(list(product(range(3), a))))
[(0, 1),
(0, 2),
(1, 1),
(1, 2),
(2, 1)
, (2, 2)] #there are 6 possible value
# range(3) represent (0,1,2)
0
from itertools import permutation
items=['x','y']
result=list(permutations(items))
print(result)
- 1
RESULT ---------------> 6
- 1
6
- 1
6
- 1
question : What is the output of this code?
program : from itertools import product
a={1, 2}
print(len(list(product(range(3), a))))
output : 6
- 1
6 is the answer
- 1
Ans 6