python product

Solutions on MaxInterview for python product by the best coders in the world

showing results for - "python product"
Anna
16 Jan 2020
1from itertools import product
2
3if __name__ == "__main__"4    arr1 = [1, 2, 3]
5    arr2 = [5, 6, 7]
6    print( list(product(arr1, arr2)) )
7
8Output
9[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
Corina
25 Oct 2016
1#from itertools import product
2
3
4def product(*args, **kwds):
5    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
6    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
7    pools = map(tuple, args) * kwds.get('repeat', 1)
8    result = [[]]
9    for pool in pools:
10        result = [x+[y] for x in result for y in pool]
11    for prod in result:
12        yield tuple(prod)
13