简单的推导表达式
''' 得到一个元素为1到9的列表如何做? ''' # ------最淳朴的写法----- li = [1,2,3,4,5,6,7,8,9,10] #-------for循环迭代写法-- li1 = [] for i in range(1,11): li1.append(i) print(li1) #-------列表推导式写法---- li2 = [i for i in range(1,11)] print(li2) #------列表推导式中加条件语句----- li3 = [i for i in range(1,100) if i>30] print(li3) li4 = [i*10 for i in range(1,11)] print(li4) # ------集合推导式--------------- se = {i for i in range(1,11)} #生成1~10的整数 #------字典推导式---------------- #如何生成这样一个字典呢:{0:"a",1:"b",2:"c"} li5 = ["a","b","c"] dic = {i:j for i,j in enumerate(li5)} print(dic) #最终结果为:{0: 'a', 1: 'b', 2: 'c'}