Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to multiply pair-wise tuple list
- tup = (2,3)
- tup[0]*tup[1]
- 6
- tuple(a*b for a, b in x)
- >>> tpl = ((2,2), (5,1), (3,2,4))
- >>> tuple(reduce(lambda x, y: x*y, tp) for tp in tpl)
- (4, 5, 24)
- >>>
- tuple(product(myTuple) for myTuple in ((2,2), (5,1), (3,2)))
- def product(cont):
- base = 1
- for e in cont:
- base *= e
- return base
- x = ((2, 2), (5, 1), (3, 2))
- y = ((2, 3), (2, 3, 5), (2, 3, 5, 7))
- def multiply_the_elements_of_several_tuples_using_a_for_loop(X):
- tuple_of_products = []
- for x in X:
- product = 1
- for element in x:
- product *= element
- tuple_of_products.append(product)
- return tuple(tuple_of_products)
- print(multiply_the_elements_of_several_tuples_using_a_for_loop(x))
- print(multiply_the_elements_of_several_tuples_using_a_for_loop(y))
- (4, 5, 6)
- (6, 30, 210)
Advertisement
Add Comment
Please, Sign In to add comment