johnchristopher
3/10/2015 - 10:35 AM

gistfile1.json

{"nbformat_minor": 0, "cells": [{"execution_count": 2, "cell_type": "code", "source": "s='12'", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 3, "cell_type": "code", "source": "map(int, s)", "outputs": [{"execution_count": 3, "output_type": "execute_result", "data": {"text/plain": "[1, 2]"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Note que `int` est invoqu\u00e9 sans les parenth\u00e8ses.", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "[int(x) for x in s]", "outputs": [{"execution_count": 4, "output_type": "execute_result", "data": {"text/plain": "[1, 2]"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "List comprehension. C'est d\u00e9j\u00e0 plus _pythonic_ comme ils disent.", "cell_type": "markdown", "metadata": {}}, {"execution_count": 14, "cell_type": "code", "source": "filter(lambda l: l == '1', s)", "outputs": [{"execution_count": 14, "output_type": "execute_result", "data": {"text/plain": "'1'"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 13, "cell_type": "code", "source": "[x for x in s if x == '1']", "outputs": [{"execution_count": 13, "output_type": "execute_result", "data": {"text/plain": "['1']"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Note que `filter` (du tryptique map/reduce/filter) ne retourne pas une liste :(.", "cell_type": "markdown", "metadata": {}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}