2
products = ['coca', 'pepsi', 'guarana', 'skol', 'brahma', 'agua', 'del valle', 'dolly', 'red bull', 'cachaça', 'vinho tinto', 'vodka', 'vinho branco', 'tequila', 'champagne', 'gin', 'guaracamp', 'matte', 'leite de castanha', 'leite', 'jurupinga', 'sprite', 'fanta']

sales = [1200, 300, 800, 1500, 1900, 2750, 400, 20, 23, 70, 90, 80, 1100, 999, 900, 880, 870, 50, 1111, 120, 300, 450, 800]

How do I get the top five products according to these two lists?

I can certainly do it just by looking, but that would be useless

from collections import Counter
top5 = Counter(produtos)
print(top5.most_common(5))

I tried this, but of course it didn't work

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • There are exactly two approaches that make sense: sort the parallel lists "in parallel" and then see what ends up at the front; or build a dictionary and then look for the largest values (which can be done by creating a `collections.Counter` **from that dictionary**). Either of those two things is a common duplicate Q&A. – Karl Knechtel Mar 02 '23 at 05:12

2 Answers2

1

Here's one way to do it manually (I'm assuming Python 3). There's probably a built-in way to do it, but I'm not aware of it, so I'll show how to manually instead

# declare the lists from your question
products = ['coca', 'pepsi', 'guarana', 'skol', 'brahma', 'agua', 'del valle', 'dolly', 'red bull', 'cachaça', 'vinho tinto', 'vodka', 'vinho branco', 'tequila', 'champagne', 'gin', 'guaracamp', 'matte', 'leite de castanha', 'leite', 'jurupinga', 'sprite', 'fanta']
sales = [1200, 300, 800, 1500, 1900, 2750, 400, 20, 23, 70, 90, 80, 1100, 999, 900, 880, 870, 50, 1111, 120, 300, 450, 800]

Next, combine them into a dictionary (credit to this answer for how)

combined = dict(zip(products, sales))

We'll store the n most common in this array

overall_most_common = []

You'll replace 2 with how many most common you want

for i in range(2):
    most_common_key = None
    most_common_quantity = -1
    # for every element
    for item in combined:
        # if this prodcut has a higher quantity than our previous-highest, replace the old-highest with the current element
        if combined[item] > most_common_quantity:
            most_common_quantity = combined[item]
            most_common_key = item
    # save the most common in our output array
    overall_most_common.append(most_common_key)

    # remove the most commom from this iteration (otherwise we'd get the same element only)
    del combined[most_common_key]

And print it

print(overall_most_common)

Or all together (remember to replace 2 with 5 if you want the five most common):

products = ['coca', 'pepsi', 'guarana', 'skol', 'brahma', 'agua', 'del valle', 'dolly', 'red bull', 'cachaça', 'vinho tinto', 'vodka', 'vinho branco', 'tequila', 'champagne', 'gin', 'guaracamp', 'matte', 'leite de castanha', 'leite', 'jurupinga', 'sprite', 'fanta']
sales = [1200, 300, 800, 1500, 1900, 2750, 400, 20, 23, 70, 90, 80, 1100, 999, 900, 880, 870, 50, 1111, 120, 300, 450, 800]

combined = dict(zip(products, sales))


overall_most_common = []

for i in range(2):
    most_common_key = None
    most_common_quantity = -1
    for item in combined:
        if combined[item] > most_common_quantity:
            most_common_quantity = combined[item]
            most_common_key = item
    overall_most_common.append(most_common_key)
    del combined[most_common_key]
print(overall_most_common)
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
cocomac
  • 518
  • 3
  • 9
  • 21
0

You can still use a Counter object if you would like.

from collections import Counter

products = ['coca', 'pepsi', 'guarana', 'skol', 'brahma', 'agua', 'del valle', 'dolly', 'red bull', 'cachaça', 'vinho tinto', 'vodka', 'vinho branco', 'tequila', 'champagne', 'gin', 'guaracamp', 'matte', 'leite de castanha', 'leite', 'jurupinga', 'sprite', 'fanta']

sales = [1200, 300, 800, 1500, 1900, 2750, 400, 20, 23, 70, 90, 80, 1100, 999, 900, 880, 870, 50, 1111, 120, 300, 450, 800]

sales_dict = dict(zip(products, sales))
sales_counter = Counter(sales_dict)
top5_sales = sales_counter.most_common(5)

print(top5_sales)

Output:

[('agua', 2750), ('brahma', 1900), ('skol', 1500), ('coca', 1200), ('leite de castanha', 1111)]
Kuroneko
  • 146
  • 6