from random import randint from mrjob.py2 import xrange # 列表筛选大于0的数 # 常用写法 data = [1, 5, -3, -2, 6, 0, 9] res = [] for x in data: if x >= 0: res.append(x) print(res) # 生成随机数 data = [randint(-10, 10) for _ in xrange(10)] print(data) # 筛选大于0的数 dataNew = filter(lambda x: x >= 0, data) print(list(dataNew)) print(data) dataNew = [x for x in data if x >= 0] # 首选 print(dataNew) # 字典 d = {x: randint(60, 100) for x in xrange(1, 21)} print(d) # 根据值过滤分数大于90的 res = {k: v for k, v in d.items() if v > 90} print(res) # 集合 print(data) s = set(data) print(s) # 筛选可整除3的数据 sNew = {x for x in s if x % 3 == 0} print(sNew)