Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get top 5 values where key total is less than or equal to X

Currently I have a list of items someone can buy as follows:

my_list = [
    ('Candy', 1.0, 20.5),
    ('Soda', 3.0, 10.25),
    ('Coffee', 1.2, 20.335),
    ('Soap', 1.2, 11.5),
    ('Spoon', 0.2, 2.32),
    ('Toast', 3.2, 12.335),
    ('Toothpaste', 3, 20.5),
    ('Creamer', .1, 5.5),
    ('Sugar', 2.2, 5.2),
]

Each item is set up like this:

('Item Name', ItemCost, ItemValue)

I have the list pulling the items with the top 5 ItemValue.

print nlargest(5, my_list, key=itemgetter(2))
>>> [
        ('Candy', 1.0, 20.5),
        ('Toothpaste', 3, 20.5),
        ('Coffee', 1.2, 20.335),
        ('Toast', 3.2, 12.335),
        ('Soap', 1.2, 11.5),
    ]

I am trying to retrieve a result where I get the top 5 total ItemValue where the top 5 total ItemCost is equal or less than 6.

Any suggestions?

like image 431
DarkMatter Avatar asked Sep 24 '18 23:09

DarkMatter


1 Answers

You can filter first, and use all following nlargest on your filtered list.

f = [(a,b,c) for (a,b,c) in my_list if b <= 6]

But for data manipulation like this, pandas can be very useful. Take, for example

df = pd.DataFrame(my_list, columns=('ItemName', 'ItemCost', 'ItemValue'))

    ItemName    ItemCost    ItemValue
0   Candy       1.0         20.500
1   Soda        3.0         10.250
2   Coffee      1.2         20.335
3   Soap        1.2         11.500
4   Spoon       0.2         2.320
5   Toast       3.2         12.335
6   Toothpaste  3.0         20.500
7   Creamer     0.1         5.500
8   Sugar       2.2         5.200

>>> df[df.ItemCost <= 6]

    ItemName    ItemCost    ItemValue
0   Candy       1.0         20.500
1   Soda        3.0         10.250
2   Coffee      1.2         20.335
3   Soap        1.2         11.500
4   Spoon       0.2         2.320
5   Toast       3.2         12.335
6   Toothpaste  3.0         20.500
7   Creamer     0.1         5.500
8   Sugar       2.2         5.200

>>> df[df.ItemCost <= 6].nlargest(n=5, columns=['ItemValue'])


    ItemName    ItemCost    ItemValue
0   Candy       1.0         20.500
6   Toothpaste  3.0         20.500
2   Coffee      1.2         20.335
5   Toast       3.2         12.335
3   Soap        1.2         11.500

If you want, you can first get the nsmallest of the ItemCost and just then get the nlargest

df.nsmallest(n=5, columns=['ItemCost']).nlargest(n=5, columns=['ItemValue'])    

    ItemName    ItemCost    ItemValue
0   Candy       1.0         20.500
2   Coffee      1.2         20.335
3   Soap        1.2         11.500
7   Creamer     0.1         5.500
4   Spoon       0.2         2.320
like image 54
rafaelc Avatar answered Sep 22 '22 06:09

rafaelc