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)