Diferencia entre revisiones de «Curso Python DGA 2011/inmersion python/chuleta»
De WikiEducator
(→Comprensión de listas) |
|||
Línea 8: | Línea 8: | ||
==Comprensión de listas== | ==Comprensión de listas== | ||
<source lang="python"> | <source lang="python"> | ||
− | >>> [str(x) for x in range(10)] | + | >>> [x**2 for x in range(10)] |
− | + | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | |
+ | >>> [x**2 for x in range(10) if x % 2] | ||
+ | [1, 9, 25, 49, 81] | ||
+ | >>> ', '.join([str(x) for x in range(10)]) | ||
+ | '0, 1, 2, 3, 4, 5, 6, 7, 8, 9' | ||
</source> | </source> |
Revisión de 03:41 6 sep 2011
Aplicar función a lista
>>> map(str, range(10)) ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] >>> ', '.join(map(str, range(10))) '0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
Comprensión de listas
>>> [x**2 for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> [x**2 for x in range(10) if x % 2] [1, 9, 25, 49, 81] >>> ', '.join([str(x) for x in range(10)]) '0, 1, 2, 3, 4, 5, 6, 7, 8, 9'