3 回答

TA貢獻1875條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超14個贊
可能最簡單的方法是利用字典對唯一鍵的強制執(zhí)行:
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))
添加回答
舉報