使一行for选择性生成列表
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list = [row for row in data if row[0] >2]
>>> [[4, 5, 6], [7, 8, 9]]
使一行for选择性生成字典
字典的items() ,返回一个(键,值)的元组列表
dict = {'key1': 7, 'key2': 9, 'key3': 11}
my_results = {key : value for key, value in dict.items() if value > 8})
>>>{'key2': 9, 'key3': 11}
dict.items()
>>>dict_items([('key1', 7), ('key2', 9), ('key3', 11)])