Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list comprehension
#1
Hi! I try to modify the function below with list comprehation.
But I don't now how to write that else secvantion

This:
def cakes(recipe, available): list_value = [ ] for key,value in recipe.items(): if key in available: a = int(available[key]) // int(value) list_value.append(a) else: return 0 list_value.sort() return list_value[0] 
In this:
def cakes(recipe, available): list_value = [int(available[key]) // int(value) for key,value in recipe.items() if key in available else return 0 ].sort() return list_value[0] 
Reply
#2
def cakes(recipe, available): values = [int(available[key]) // int(value) for key, value in recipe.items() if key in available] if len(values) > 0: values.sort() return values[0] return 0
3lnyn0 likes this post
Reply
#3
Quote:[int(available[key]) // int(value) for key,value in recipe.items() if key in available else return 0 ].sort()

The sort method of a list, does not return the list. Instead, the list is sorted inline and returns None, which is assigned to list_value.

You can wrap around the whole list comprehension with the sorted function. The sorted function returns a new list, where the original list is not modified.

If you want to get the highest number of a sequence, use the max function.
To get the lowest number, use min.

Example:
from __future__ import annotations def cakes(recipe: dict, available: dict) -> int | None: values = [ int(available[key]) // int(value) for key, value in recipe.items() if key in available ] if values: return min(values)
Annotations: They are not mandatory, but can give you a hint, which types should be used and which types return. For example, an empty recipe will return None. A valid recipe will return an int.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
You can avoid checking if values is empty by using the 'default' argument in min()
>>> min([], default=0) 0
So
def cakes(recipe, available): return min( (int(available[key]) // int(value) for key, value in recipe.items() if key in available), default=0)
Reply
#5
@Gribouillis, thanks.

The blacked Version is easier to read:
def cakes(recipe, available): return min( ( int(available[key]) // int(value) for key, value in recipe.items() if key in available ), default=0, )
The parenthesis for the Generator Expression is not mandatory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List comprehension not working right Cris9855 3 1,708 Nov-04-2024, 03:46 PM
Last Post: DeaD_EyE
  Problem with List Comprehension in Python laurawoods 3 1,973 Aug-12-2024, 06:26 AM
Last Post: Pedroski55
  List Comprehension Issue johnywhy 5 3,087 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 2,728 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 5,086 Apr-02-2023, 06:31 PM
Last Post: tester_V
  List comprehension used differently coder_sw99 3 3,326 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 4,941 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 3,846 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 3,861 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 12,956 Dec-18-2020, 10:45 PM
Last Post: muzikman

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.