Advertisement
teslariu

funciones de orden superior e integradas

Aug 16th, 2023
1,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # funciones anónimas o inline o lambda
  4.  
  5. # Función lambda que suma dos numeros:
  6. suma = lambda x,y : x+y
  7. print(suma(14,16))
  8.  
  9.  
  10.  
  11. # Funcion de orden superior: una funcion que llama a otra
  12. # ejemplos: map y filter
  13.  
  14. # map "mapea" una funciòn sobre un iterable, o sea, aplica la funciòn
  15. # repetidas veces tomando los elementos del iterable como argumentos
  16. # de 1 a la vez
  17.  
  18. nombres = ["hUgO", "GeRardo", "luisA", "javier"]
  19. nombres_ok = list(map(lambda nombre:nombre.lower().capitalize(), nombres))
  20. print(nombres_ok)
  21.  
  22. # filter es parecida a map pero devuelve True or False
  23. def multiplo_3(n):
  24.     if not n % 3:
  25.         return True
  26.     else:
  27.         return False
  28.  
  29. numeros = [1,2,4,6,7,8,10,12,10,90,153,125]
  30. # multiplos = list(filter(multiplo_3,numeros))
  31. # print(multiplos)
  32.  
  33. multiplos = list(filter(lambda n: True if not n%3 else False, numeros))
  34.  
  35. # Otras funciones integradas
  36. # zip(): permite juntar varias colecciones y recorrerlas todas juntas
  37. paises = ["Francia", "Argentina", "Peru", "Italia"]
  38. ciudades = ["Niza", "Rosario", "Lima", "Sicilia"]
  39.  
  40. for ciudad, pais in zip(ciudades, paises):
  41.     print(ciudad, pais)
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement