1
votes

I have two lists: one contains the products and the other one contains their associated prices. The lists can contain an undefined number of products. An example of the lists would be something like:

  • 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']

I want to be able to remove all the duplicates from the products list and keep only the cheapest price associated with the unique products in the price list. Note that some products might have the same price (in our example the Pineapple).

The desired final lists would be something like:

  • Products : ['Apple', 'Orange', 'Banana', 'Peach', 'Pineapple']
  • Prices: ['1.00', '3.00', '0.50', '2.00', '1.00']

I would like to know the most effective way to do so in Python. Thank you

6

6 Answers

3
votes
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]
1
votes

Probably the simplest way is to take advantage of dictionaries' enforcement of unique keys:

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))
1
votes

What about this:

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())
0
votes

Not the shortest solution, but it illustrates the point: Suppose that your lists are products and prices, respectively. Then:

lookup = dict()    
for prod, price in zip(products, prices):
    if prod not in lookup:
        lookup[prod] = price
    else:
        lookup[prod] = min(price, lookup[prod])

At this point, the lookup dict contains each of your products, and its minimal price. A dict is certainly a better data structure for this than two lists; if you really want to have this as two separate lists instead, you can do something like this:

new_prods = []
new_prices = []
for prod, price in lookup.items():
    new_prods.append(prod)
    new_prices.append(price)
0
votes
>>> 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']
>>> dic = OrderedDict()
>>> for x,y in zip(products,prices):
...     dic.setdefault(x, []).append(y)
...     
>>> dic.keys()
['Apple', 'Orange', 'Banana', 'Peach', 'Pineapple']
>>> [min(val, key = float) for val in dic.values()]
['1.00', '3.00', '0.50', '2.00', '1.00']
0
votes

You can use a dictionary to do this:

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']

Prices=[float(price) for price in Prices]

di={}
for prod,price in zip(Products,Prices):
    di.setdefault(prod,[]).append(price)

for key,val in di.items():
    di[key]=min(val)

print di

Prints {'Orange': 3.0, 'Pineapple': 1.0, 'Apple': 1.0, 'Peach': 2.0, 'Banana': 0.5}

If you want two lists in the same order, you can do this:

from collections import OrderedDict

new_prod=OrderedDict.fromkeys(Products).keys()
new_prices=[di[item] for item in new_prod]

Prints:

['Apple', 'Orange', 'Banana', 'Peach', 'Pineapple']
[1.0, 3.0, 0.5, 2.0, 1.0]