3 回答

TA贡献1875条经验 获得超3个赞
from collections import OrderedDict
products = ['Apple', 'Apple', 'Apple', 'Orange', 'Banana', 'Banana', 'Peach', 'Pineapple', 'Pineapple']
prices = ['1.00', '2.00', '1.50', '3.00', '0.50', '1.50', '2.00', '1.00', '1.00']
min_prices = OrderedDict()
for prod, price in zip(products, prices):
min_prices[prod] = min(float(price), min_prices.get(prod, float('inf')))
>>> print min_prices.keys(), min_prices.values()
['Apple', 'Orange', 'Banana', 'Peach', 'Pineapple'] [1.0, 3.0, 0.5, 2.0, 1.0]

TA贡献1807条经验 获得超9个赞
那这个呢:
prices = map(float,prices)
r={}
for k,v in zip(products,prices):
if v < r.setdefault(k,float('inf')):
r[k] = v
products,prices = r.keys(),map(str,r.values())

TA贡献2065条经验 获得超14个赞
可能最简单的方法是利用字典对唯一键的强制执行:
from operator import itemgetter
Products = ['Apple', 'Apple', 'Apple', 'Orange', 'Banana', 'Banana', 'Peach', 'Pineapple', 'Pineapple']
Prices = ['1.00', '2.00', '1.50', '3.00', '0.50', '1.50', '2.00', '1.00', '1.00']
final = dict(sorted(zip(Products, Prices), key=itemgetter(1), reverse=True))
添加回答
举报