+ 1
Can we get a product of a list and a tuple or set? Means--- a= {2,3}. Print(list(product(a, range(3)))). If yes then how?
Is product of tuple and list possible?
2 Respostas
+ 5
You can also try this::
# using a function:
s = {2,3} # s can be any iterable
def prod(x, y):
return x * y
print(prod(*s))
#or use a for loop:
res = 1
for i in s:
res *= i
print(res)
# or in a shorter way if you like this more:
res = 1
for i in s: res *= i
print(res)
[Edit]: if set has always the same number of elements:
print(list(s)[0]*list(s)[1]) # if s is a set
print(s[0] * s[1]) # if s is tuple or list
+ 1
from functools import reduce
from itertools import chain
print(reduce((lambda x, y: x*y), chain([3, 5, 7], {2, 4, 6}, (1, 8))))