Learning about packaged salads

""" JavaScript to extract on-page data (used in a DevTools console): var cells = document.querySelectorAll('td:nth-of-type(2)') cells = Array.from(cells) cells.map(el => el.innerHTML) """ # name, weight (g), calories (kcal), fat (g), saturates (g), carbohydrate (g), sugars (g), fibre (g), protein (g), salt (g), price (pounds) # All values are for a portion of 100g tesco_packaged_salads = """ Tesco Iceberg Lettuce Each,350,23.30,0.3,0.04,3.40,3.20,1.90,0.80,0.02,0.5 Tesco Little Gem Lettuce Twin Pack,180,16.30,0.50,0.10,1.70,1.70,0.90,0.80,0.01,0.85 Tesco Peppery Babyleaf Rocket Salads,90,34.82,0.30,0.18,3.20,0.30, 3.30,3.18,0.14,1.25 Tesco Sweet Gem Lettuce,240,16.30,0.50,0.10,1.70,1.70,0.90,0.80,0.01,1.00 Tesco Watercress Spinach And Rocket,80,34.38,0.50,0.11,3.20,0.70,1.70,3.42,0.25,1.00 Nightingale Farms Round Lettuce,-,16.30,0.50,0.10,1.70,1.70,0.90,0.80,0.01,- Tesco Babyleaf Salad,60,18.30,0.30,0.00, 1.70,0.40,0.60,1.90,0.22,0.59 Florette Classic Crispy Salad,170,18,0.3,0.0,0.9,0.5,3.0,1.5,0.04,1.00 Tesco Sweet And Crunchy Salad,370,23.30,0.30,0.04,3.40,3.20,1.90,0.80,0.02,1.00 Tesco Romaine Lettuce Hearts Twin Pack,280,16.30,0.50,0.10,1.70,1.70,0.90,0.80,0.01,1.00 Tesco Iceberg,260,14.30,0.30,0.00,1.90,1.90,0.60,0.70,0.00,1.00 Tesco Baby Spinach,240,29.80,0.80,0.10,1.50,1.50,2.70,2.80,0.35,1.50 Florette Mixed Salad,150,16,0.3,0.1,0.7,0.7,2.7,1.2,0.05,1.00 Fresh And Naked Mixed Little Leaves,90,19,0.6,0.1,1.7,1.6,1.4,1.6,0.14,0.95 Tesco Wild Rocket,60,33.30,0.30,0.10,2.20,0.10,3.50,3.70,0.21,0.89 Fresh And Naked Just Wild Rocket,60,15,0.5,0.1,1.7,1.7,0.9,0.8,0.01,0.95 Tesco Watercress,85,28.58,0.30,0.09,2.30,0.30,2.50,2.92,0.34,1.30 Tesco Leafy Butterhead Salad,100,16.60,0.60,0.10,0.90,0.70,1.00,1.40,0.03,1.00 Tesco Beetroot Salad,160,32.60,0.40,0.00,3.90,3.50,2.50,2.10,0.24,1.30 Tesco Simple Salad,135,26.98,0.46,0.15,3.86,2.43,1.52,1.09,0.10,1.00 Tesco Baby Spinach,130,29.80,0.80,0.10,1.50,1.50,2.70,2.80,0.35,0.89 Florette Babyleaf And Rocket,80,20,0.5,0.1,0.9,0.2,2.0,2.1,0.18,1.10 Tesco Iceberg Lettuce,175,18.90,0.10,0.00,3.00,2.00,1.20,0.90,0.03,0.59 Tesco Sweet Crisp Salad,128,31.25,0.95,0.00,2.57,2.10,1.67,2.27,0.08,1.00 Tesco House Salad,120,30.20,0.60,0.12,3.47,1.90,2.10,1.68,0.05,1.00 Tesco Grated Carrot,200,42.60,0.40,0.10,7.40,7.20,3.90,0.40,0.06,0.75 Tesco Babyleaf Salad,140,18.30,0.30,0.00,1.70,0.40,0.60,1.90,0.22,1.50 Tesco Nightingale Crispy Mixed Leaf Salad,120,19.00,0.20,0.10,1.70,1.00,2.20,1.50,0.03,0.69 Tesco Creamy Caesar Salad,262,154.10,12.10,1.84,7.30,1.90,1.20,3.40,0.47,1.85 Florette Classic Crispy Salad,85,18,0.3,0.0,0.9,0.5,3.0,1.5,0.04,0.89 Tesco Aromatic Babyleaf Herb Salad,105,23.64,0.40,0.11,0.70,0.20,3.50,2.56,0.27,1.50 Tesco Pink Slaw,140,54.23,0.62,0.29,10.35,8.97,2.35,0.62,0.84,1.00 Fresh&Naked Spicy Mix,90,16,0.4,0.1,0.4,0.4,1.5,2.7,0.1,0.95 Tesco Crunchy Salad,242,25.60,0.40,0.10,3.10,2.70,2.40,1.20,0.02,2.00 Tesco Wild Rocket,120,33.30,0.30,0.10,2.20,0.10,3.50,3.70,0.21,1.50 Florette Classic Salad Bowl,160,25,0.3,0.1,3.6,2.9,2.0,0.8,0.04,1.00 Tesco Alfresco Salad,250,26.20,0.40,0.10,3.90,3.70,1.90,0.80,0.02,1.25 Florette Summer Mix Salad Bag,140,17,0.4,0.1,0.8,0.7,2.4,1.3,0.09,1.00 Tesco Organic Watercress Spinach Rocket,100,28.70,0.90,0.20,1.30,0.70,1.90,2.90,0.03,1.50 Tesco Chinese Leaf Each,400,16.40,0.20,0.10,2.00,2.00,1.30,1.00,0.07,1.25 Nightingale Farms Sweet Leaf Salad,260,23.80,0.20,0.00,3.60,3.10,1.80,1.00,0.03,0.69 Tesco Party Salad,500,36.44,0.96,0.30,4.60,2.60,2.10,1.30,0.02,3.00 Tesco Red Slaw,140,54.17,0.47,0.14,10.66,8.98,2.16,0.73,0.84,1.00 Tesco Organic Romaine Lettuce,250,16.30,0.50,0.10,1.70,1.70,0.90,0.80,0.01,1.30 Tesco Peppery Babyleaf Salad Bowl,95,22.70,0.30,0.10,2.60,2.40,1.60,1.60,0.03,1.00 Tesco Red Gem Lettuce 2 Pack,-,16,0.8,1.7,1.7,0.5,0.1,0.9,0.1,1.00 Tesco Chicken Caesar Salad,178,177.30,11.10,2.10,6.80,1.50,1.30,11.90,0.60,2.50 Tesco Finest Hot Rocket And Red Mustard Salad,85,23.60,0.40,0.10,1.20,0.30,1.20,3.20,0.07,1.31 Tesco Finest Pea Shoot And Garlic Chives,85,27.20,0.20,0.00,3.60,2.20,0.30,2.60,0.18,1.31 Tesco Organic Watercress,75,25.60,1.00,0.30,0.40,0.40,1.50,3.00,0.03,1.50 Good4u Super Sprouts,60,40,1.2,0.8,1.3,0.6,2.5,4.7,0.04,1.50 Tesco Beetroot And Goats Cheese Salad,260,119.70,6.90,2.80,8.20,3.30,2.80,4.80,0.45,2.50 Tesco Garden Salad,240,22.60,0.40,0.10,2.80,1.90,1.30,1.30,0.02,2.00 Tesco Pesto Salad Kit,170,165.60,14.40,2.34,2.60,1.10,1.60,5.60,0.49,1.85 Tesco Italian Salad,255,159.80,11.60,3.42,6.10,2.50,1.90,6.80,0.95,3.25 Florette Sunblush Tomato Olive Salad,213,68,5.0,0.4,3.4,2.4,2.5,1.1,1.03,2.00 Tesco Tomato And Mozzarella Salad,185,121.20,7.40,2.90,8.40,3.70,1.30,4.60,0.50,2.50 Tesco Fajita Chicken Salad,200,88.10,3.90,1.00,4.10,1.50,2.30,8.00, 0.30,2.50 Tesco Finest Baby Chard. Salad,85,22.50,0.30,0.00,1.80,0.40,1.70,2.30,0.32,1.31 Florette Taste Of Italy Side Salad Bowl,125,156,9.4,4.3,10.4,2.9,1.4,6.9,1.40,2.00 Tesco Seasonal Lettuce,180,13.90,0.10,0.01,1.40,1.40,1.30,1.20,0.02,1.00 Tesco Morrocan Salad Bowl,270,121.30,6.70,2.26,9.50,4.60,1.90,4.80,0.57,3.25 """ """ Questions: Which salad gives the most calories for the price? (use salad weight) Which has the most saturated fat? Sugar? Fiber? Protein? Salt? """ from heapq import heappush, nlargest, nsmallest from operator import itemgetter h = [] features = 7 alltup, allh = [() for i in range(features)], [[] for i in range(features)] rows = tesco_packaged_salads.split('\n')[1:-1] for row in rows: data = row.split(',') data[3:-1] = map(float, data[3:-1]) name, weight, calories, fat, saturates, carbohydrate, sugars, fibre, protein, salt, price = data if '-' not in (weight, calories, price): weight = int(weight) calories = float(calories) price = float(price) calories_per_price = (weight * calories) / (100 * price) heappush(h, (name, calories_per_price)) for i in range(features): alltup[i] = (name, data[3:-1][i]) if alltup[i] not in allh[i]: heappush(allh[i], alltup[i]) show_salads = 5 titles = ['Total calories/price', 'Salads with most saturated fat', 'Salads with the least amount of saturated fat', 'Salads with most sugar', 'Salads with least sugar', 'Salads with most fibre', 'Salads with most protein', 'Salads with least salt'] of_interest = [nlargest if typ else nsmallest for typ in (1, 1, 0, 1, 0, 1, 1, 0)] criteria = [-1, 1, 1, 3, 3, 4, 5, 6] for i, title in enumerate(titles): print('\n%s:' % (title)) useh = h if not i else allh[criteria[i]] suf = '' if not i else 'g' for name, cal in of_interest[i](show_salads, useh, itemgetter(1)): print("%.2f%s %s" % (cal, suf, name)) """ Total calories/price: 218.24 Tesco Creamy Caesar Salad 163.10 Tesco Iceberg Lettuce Each 152.17 Tesco Pesto Salad Kit 126.24 Tesco Chicken Caesar Salad 125.38 Tesco Italian Salad Salads with most saturated fat: 4.30g Florette Taste Of Italy Side Salad Bowl 3.42g Tesco Italian Salad 2.90g Tesco Tomato And Mozzarella Salad 2.80g Tesco Beetroot And Goats Cheese Salad 2.34g Tesco Pesto Salad Kit Salads with the least amount of saturated fat: 0.00g Florette Classic Crispy Salad 0.00g Nightingale Farms Sweet Leaf Salad 0.00g Tesco Beetroot Salad 0.00g Tesco Iceberg 0.00g Tesco Finest Pea Shoot And Garlic Chives Salads with most sugar: 8.98g Tesco Red Slaw 8.97g Tesco Pink Slaw 7.20g Tesco Grated Carrot 4.60g Tesco Morrocan Salad Bowl 3.70g Tesco Alfresco Salad Salads with least sugar: 0.10g Tesco Wild Rocket 0.20g Florette Babyleaf And Rocket 0.20g Tesco Aromatic Babyleaf Herb Salad 0.30g Tesco Finest Hot Rocket And Red Mustard Salad 0.30g Tesco Watercress Salads with most fibre: 3.90g Tesco Grated Carrot 3.50g Tesco Aromatic Babyleaf Herb Salad 3.50g Tesco Wild Rocket 3.30g Tesco Peppery Babyleaf Rocket Salads 3.00g Florette Classic Crispy Salad Salads with most protein: 11.90g Tesco Chicken Caesar Salad 8.00g Tesco Fajita Chicken Salad 6.90g Florette Taste Of Italy Side Salad Bowl 6.80g Tesco Italian Salad 5.60g Tesco Pesto Salad Kit Salads with least salt: 0.00g Tesco Iceberg 0.01g Fresh And Naked Just Wild Rocket 0.01g Nightingale Farms Round Lettuce 0.01g Tesco Organic Romaine Lettuce 0.01g Tesco Sweet Gem Lettuce """