以下为某商店的商品销售信息: sales = [ ['订单号','商品类别编号','商品名称','单价','销售数量','退货数量'], ['1058032','01','蛋糕80g',6.78,1,0], ['1058032','02','雀巢1+2咖啡原味15g*7条',10.50,10,2], ['1033673','02','雪碧清爽柠檬味汽水500ml',2.98,58,0], ['1033673','01','爱达乐全麦吐司面包180g',5.48,20,0], ['1033675','01','菠萝泡芙蛋糕100g',6.98,10,0], ['1029456','03','卫龙牛味棒20g',0.50,20,2], ['1029456','03','洽洽香瓜子200g',7.00,12,0], ['1029456','02','雪碧清爽柠檬味汽水500ml',3.00,100,0] ] 1.请编写函数实现计算每个商品的销售额,形参为销售信息列表,返回带有销售额的列表 2.主程序:调用函数计算每个商品的销售额,然后统计每种商品类别的商品数量和总销售额, 按总销售额从高到底输出每种商品类别的总销售额及其排名 输出包含商品数量最多的商品类别
时间: 2023-08-06 20:20:37 浏览: 48
这是一道之前已经回答过的问题,回答如下:
1. 实现计算每个商品的销售额的函数:
```python
def calc_sales(sales):
sales_with_revenue = []
for i in range(len(sales)):
if i == 0:
# 添加表头
sales_with_revenue.append(sales[i] + ['销售额'])
else:
# 计算销售额并添加到列表中
revenue = (sales[i][3] * sales[i][4]) - (sales[i][3] * sales[i][5])
sales_with_revenue.append(sales[i] + [revenue])
return sales_with_revenue
```
2. 主程序:
```python
# 商品销售信息
sales = [
['订单号','商品类别编号','商品名称','单价','销售数量','退货数量'],
['1058032','01','蛋糕80g',6.78,1,0],
['1058032','02','雀巢1+2咖啡原味15g*7条',10.50,10,2],
['1033673','02','雪碧清爽柠檬味汽水500ml',2.98,58,0],
['1033673','01','爱达乐全麦吐司面包180g',5.48,20,0],
['1033675','01','菠萝泡芙蛋糕100g',6.98,10,0],
['1029456','03','卫龙牛味棒20g',0.50,20,2],
['1029456','03','洽洽香瓜子200g',7.00,12,0],
['1029456','02','雪碧清爽柠檬味汽水500ml',3.00,100,0]
]
# 调用函数计算每个商品的销售额
sales_with_revenue = calc_sales(sales)
# 统计每种商品类别的商品数量和总销售额
categories = {}
for i in range(1, len(sales_with_revenue)):
category = sales_with_revenue[i][1]
revenue = sales_with_revenue[i][6]
if category in categories:
categories[category]['count'] += 1
categories[category]['revenue'] += revenue
else:
categories[category] = {'count': 1, 'revenue': revenue}
# 按总销售额从高到低输出每种商品类别的总销售额及其排名
sorted_categories = sorted(categories.items(), key=lambda x: x[1]['revenue'], reverse=True)
print('每种商品类别的总销售额及其排名:')
for i, category in enumerate(sorted_categories):
print(f'第{i+1}名:{category[0]},总销售额:{category[1]["revenue"]:.2f}元')
# 输出包含商品数量最多的商品类别
max_count_category = max(categories.items(), key=lambda x: x[1]['count'])
print(f'商品数量最多的商品类别是:{max_count_category[0]},共有{max_count_category[1]["count"]}个商品')
```
阅读全文