如何修改代码可以计算今天6个不同价格的平均价格?
prices = [10, 12, 15, 18, 20, 22]
average_price = sum(prices) / len(prices)
print(average_price)
修改代码可以计算今天6个不同价格的平均价格,并使用列表 comprehension:
prices = [10, 12, 15, 18, 20, 22]
average_price = sum([x for x in prices if x <= 18]) / len(prices)
print(average_price)
修改代码可以计算今天6个不同价格的平均价格,并使用 filter 和 reduce:
prices = [10, 12, 15, 18, 20, 22]
average_price = sum(filter(lambda x: x <= 18, prices)) / len(prices)
print(average_price)
```