Guest User

Untitled

a guest
May 22nd, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. How to multiply pair-wise tuple list
  2. tup = (2,3)
  3. tup[0]*tup[1]
  4. 6
  5.  
  6. tuple(a*b for a, b in x)
  7.  
  8. >>> tpl = ((2,2), (5,1), (3,2,4))
  9. >>> tuple(reduce(lambda x, y: x*y, tp) for tp in tpl)
  10. (4, 5, 24)
  11. >>>
  12.  
  13. tuple(product(myTuple) for myTuple in ((2,2), (5,1), (3,2)))
  14.  
  15. def product(cont):
  16. base = 1
  17. for e in cont:
  18. base *= e
  19. return base
  20.  
  21. x = ((2, 2), (5, 1), (3, 2))
  22. y = ((2, 3), (2, 3, 5), (2, 3, 5, 7))
  23.  
  24. def multiply_the_elements_of_several_tuples_using_a_for_loop(X):
  25. tuple_of_products = []
  26.  
  27. for x in X:
  28. product = 1
  29.  
  30. for element in x:
  31. product *= element
  32.  
  33. tuple_of_products.append(product)
  34.  
  35. return tuple(tuple_of_products)
  36.  
  37. print(multiply_the_elements_of_several_tuples_using_a_for_loop(x))
  38. print(multiply_the_elements_of_several_tuples_using_a_for_loop(y))
  39.  
  40. (4, 5, 6)
  41. (6, 30, 210)
Advertisement
Add Comment
Please, Sign In to add comment