Friday, 28 September 2018

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?



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

No comments:

Post a Comment