Functional Python with Lists -
the problem simple, give list , condition, if @ least 1 item in list not obey condition program should return false, if of them obey condition should return true, yet i can use 1 return instruction code:
def todos_lista(lista, guarda): x in lista: return(false if guarda(x)==false else true)
you should use all:
def todos_lista(lista, guarda): return all(guarda(x) x in lista)
or in more functional way:
def todos_lista(lista, guarda): return all(map(guarda, lista))
for example range 0 9 (range(10)
):
>>> all(x < 10 x in range(10)) true >>> all(x < 9 x in range(10)) false >>> all(map(lambda x: x < 9, range(10))) false >>> all(map(lambda x: x < 10, range(10))) true
Comments
Post a Comment