# 列表推导 # l = [‘鸡蛋%s‘ %i for i in range(10)] # print(l) # l = [] # for i in range(10): # l.append(‘鸡蛋%s‘ %i) # print(l) #生成器表达式,括号不一样,且返回值不一样 # g = (i for i in range(10)) # print(g) #生成器 # for i in g: # print(i) # g1 = (i*i for i in range(10)) # print(list(g1)) #强制转换为list,占内存,不推荐 # 字典推导式 # 例一:将一个字典的key和value对调 # mcase = {‘a‘: 10, ‘b‘: 34} # mcase_frequency = {mcase[k]:k for k in mcase} # print(mcase_frequency) #集合推导式 #将列表去重 # s = {x*x for x in [1,-1,2]} # s1 =list(s) # print(s1)
原文:https://www.cnblogs.com/hhsh/p/9557495.html