|
发表于 2021-9-11 19:20:13
|
显示全部楼层
- dict1 = {}
- while True:
- temp = input('水果名和消费额:')
- if temp == 'None':
- break
- fruit, price = temp.split()
- price = float(price)
- if fruit in dict1:
- dict1[fruit] = [dict1[fruit][0] + 1, dict1[fruit][1] + price]
- else:
- dict1[fruit] = [1, price]
- ##dict1 = {'apple': [2, 30.0], 'pear': [2, 10.0], 'banana': [1, 10.0], 'orange': [1, 30.0]}
- list1 = [[i, *j] for i, j in dict1.items()]
- list1.sort(key = lambda x:(-x[2], -x[1]))
- for each in list1:
- print('%s %s %.2f'%(each[0], each[1], each[2]))
- amount = sum(map(lambda x:x[2], list1)) / sum(map(lambda x:x[1], list1))
- print('%.2f'%amount)
- list1.sort(key = lambda x: -x[1])
- max1 = list1[0][1]
- for each in list1:
- if each[1] != max1:
- break
- print(each[0], end = ' ')
复制代码 |
|