代碼:python
In [1]: lists = [1, 2, 3, 4, 5] In [2]: l_even = [i for i in lists if i%2 ==0] In [3]: l_even Out[3]: [2, 4] In [4]:
這個代碼能夠拆解成這樣子作:web
In [1]: lists = [1,2,3,4,5] In [2]: l_even = [] In [3]: for i in lists: ...: if i%2==0: ...: l_even.append(i) ...: In [4]: l_even Out[4]: [2, 4]
這樣咱們就能夠更好的瞭解這個列表表達式的意思了。在學習或者工做中千萬不要刻意追求複雜的列表解析式。由於咱們要追The Zen of Python.app