我想编辑这个函数,这样就没有
for
循环:仅使用映射、lambda和reduce。
它将以下列表作为输入:
orders = [[1, ("5464", 4, 9.99), ("8274", 18, 12.99), ("9744", 9, 44.95)],
[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
[4, ("8732", 7, 11.99), ("7733", 11, 18.99), ("88112", 5, 39.95)]]
并返回所需的输出:
['9744', 809.1]
然而,它仍然存在
对于
循环,我怎样才能摆脱它们?
def max_book_product(orders):
dictionary = {}
for order in orders:
for book in order[1]:
if book[0] in dictionary:
dictionary[book[0]] += book[1] * book[2]
else:
dictionary[book[0]] = book[1] * book[2]
sorted_items = sorted(dictionary.items(), key=lambda tup: tup[1])
return list(sorted_items.pop())