0
How do I solve this pls 🙏
So I studied the filter function and I'm trying to return the FALSE part too. For example def pass_grades(grades): return grades > 50 scores = [10,30,50,70,22, 90] passers = list(filter(pass_grades(scores) print(passers) The output will be all pass grades in a list. But what If I want to return pass grade in one list and also failed grades in another list
4 ответов
+ 6
Elijah Nwalozie ,
this task can be done with filter as you can see in the attached code. instead of using a regular function in filter(), it is easier to use a lambda function to solve this task.
https://code.sololearn.com/cgPGjI9UbQfy/?ref=app
+ 3
`filter` is used to sort out stuffs that doesn't match a criteria from an iterable. I don't think it makes sense to use `filter` to split contents of an iterable.
If you want to separate the ones who pass from those who fails, then you can prepare 2 extra `list` objects into which you will append stuffs as you iterate through the <scores> iterable, sorting out the stuffs as you go.
+ 1
you could use reduce() function from functools module to get both lists at once check...
https://code.sololearn.com/cECfhVYWjJ2C/?ref=app
if you want to include 50 in result of second list (True == 1), then just change comparison operator to >= instead of > in 'fs' lambda used ('fs' stand for shortcut of 'filter_split') ;)
more about reduce():
https://docs.python.org/3/library/functools.html
https://thepythonguru.com/python-builtin-functions/reduce/
https://realpython.com/python-reduce-function/
https://www.geeksforgeeks.org/reduce-in-python/
0
Completa el código para iterar a través de la lista grades y mostrar solo los valores mayores a 50
for
grade
in
grades:
if
grade <= 50:
break >=50:
print(grade)