今天帅气的易哥和大家分享的是Pyton的高级特性,希望大家能和我一起学习这门语言的魅力. Python高级特性之:List Comprehensions.Generator.Dictionary and set comprehension(su013171165) 我们在需要循环处理数据的时候,往往都会用range(n)这个方法生成list但是如果需要生成奇数list或者其他list怎么办呢?这就是我今天要讲的List Comprehensions. 一.List Comprehensions(…
>>> import os >>> [d for d in os.listdir(r"d:\temp")] ['0.png', '0.xml', '01.png', 'performance'] >>> [x * x for x in range(1, 11) if x % 2 == 0] [4, 16, 36, 64, 100] >>> [m + n for m in "AB" for n i…
概述 [x *x for x in range(1,11)][k+'='+v for k,v in d.items()] [s.lower() for s in L] 详解 1.单层迭代 >>> [x *x for x in range(1,11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> [x*x for x in range(1,11) if x%2 ==0] [4, 16, 36, 64, 100] 2.双层for循环…