列表推到式
print([i for i in range(10)]) 执行结果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print([i*2 for i in range(10)]) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
egg_list=["鸡蛋%s" %i for i in range(10)] print(egg_list) 执行结果: ['鸡蛋0', '鸡蛋1', '鸡蛋2', '鸡蛋3', '鸡蛋4', '鸡蛋5', '鸡蛋6', '鸡蛋7', '鸡蛋8', '鸡蛋9']
生成器表达式
g = (i for i in range(10)) print(g) for i in g: print(i)