我目前有:
def product_of_tuples(nums_list):
'''Receives a list of tuples of two or more numbers. Returns
a list of the products of the numbers in each tuple. (Note:
the product of a sequence of numbers is obtained by multiplying
them together.)'''
result = []
for numbers in nums_list:
*# ''' need something here '''*
for num in numbers:
multi_number = num * num
result.append(multi_number)
return result
运行时
print(product_of_tuples([(1, 5), (6, 1), (2, 3, 4)]))
预期输出应为[5, 6, 24]
任何建议都将受到赞赏:)
答案 0 :(得分:1)
你搞砸了内心。由于这是乘法,你需要每次将累加器重置为1,然后你需要依次将它乘以每个数字。
答案 1 :(得分:1)
from operator import mul
def product_of_tuples(nums_list):
'''Receives a list of tuples of two or more numbers. Returns
a list of the products of the numbers in each tuple. (Note:
the product of a sequence of numbers is obtained by multiplying
them together.)'''
return [reduce(mul, i) for i in nums_list]
答案 2 :(得分:1)
我认为你应该逐个乘以每个元组中的数字,但是num * num给出平方。我可以试试这个:
def product_of_tuples(nums_list):
result = []
for tuple in nums_list:
s = 1
for item in tuple:
s *= item
result.append(s)
return result