##### 得到列表```list_x = [1,5,-3,-2,6,0,9]```中的正数 通常做法: ```python list_x = [1,5,-3,-2,6,0,9] res = [] for i in list_x: if(i > 0): res.append(i) print(res) ------------------------------------------ 输出: [1, 5, 6, 9] ``` 一般做法:使用```filter```类和```lambda匿名函数``` ```python list_x = [1,5,-3,-2,6,0,9] x = filter(lambda x:x>0,list_x) print(x) ----------------------------------------- 输出: [1, 5, 6, 9] ``` 使用列表解析,这种方法比前两种执行速度更快。*推荐* ```python list_x = [1,5,-3,-2,6,0,9] x = [x for x in list_x if x>0] ------------------------------------------------ 输出: [1, 5, 6, 9] ``` ##### 得出字典{'a':79,'b':89,'c':90}中值大于等于90的值 使用字典解析: ```python data = {'a':79,'b':89,'c':90,'d':92} d = {x:y for x,y in data1.items() if y>=90} print(d) ------------------------------------ 输出: {'c': 90, 'd': 92} ``` ##### 筛选出集合{77,33,89,32,30}中能被3整除的元素 使用集合解析 ```python data = {77,33,89,32,30} d = {x for x in data2 if x %3 == 0} print(d) ------------------------------------------------ 输出: {33, 30} ```