标签:优化 int return range 过程 res 版本 复习 pytho
""" #复习 lst1 = [1,5] lst2 = [2,6] lst3 = [3,7] lst4 = [4,8] it = zip(lst1,lst2,lst3,lst4) print(list(it)) # [(1, 2), (3, 4), (5, 6), (7, 8)] # n = 2 lst1 = [1 , 3 , 5 , 7 ,9] listvar[0::2] # 0 2 4 6 8 lst2 = [2 , 4 , 6 , 8 ] listvar[1::2] it = zip(lst1,lst2) print(list(it)) #listvar = [[1,2,3],[4,5,6],[7,8,9]] n=3 lst1 = [1,4,7] listvar[0::3] # 0 3 6 lst2 = [2,5,8] listvar[1::3] lst3 = [3,6,9] listvar[2::3] it = zip(lst1,lst2,lst3) #listvar = [[1,2,3,4],[5,6,7,8]] lst1 = [1,5,9] listvar[0::4] # 0 4 8 lst2 = [2,6] listvar[1::4] 2 6 lst3 = [3,7] listvar[2::4] lst4 = [4,8] listvar[3::4] it = zip(lst1,lst2,lst3,lst4) range(4) """ # n = 2 # lst1 = [1 , 3 , 5 , 7 ,9] #listvar[0::2] # 0 2 4 6 8 # lst2 = [2 , 4 , 6 , 8 ] #listvar[1::2] # it = zip(lst1,lst2) # print(list(it)) # zip(listvar[0::2],listvar[1::2]) listvar = [1,2,3,4,5,6,7,8,9] n = 2 res = [listvar[i::n] for i in range(n)] print(res) n = 3 res = [listvar[i::n] for i in range(n)] print(res) n = 4 res = [listvar[i::n] for i in range(n)] print(res) # 把大列表当中的每一个小列表一个一个的拿出来当成zip的参数进行赋值 # 过程化版本 n = 5 it = zip(*[listvar[i::n] for i in range(n)]) print(list(it)) # 普通版本 def func(listvar,n): return zip(*[listvar[i::n] for i in range(n)]) res = func(listvar,2) print(list(res)) # 优化版本 func = lambda listvar,n : zip( *[ listvar[i::n] for i in range(n) ] ) it = func(listvar,3) print(list(it)) ‘‘‘ # 复习 * 和 ** 的魔术用法(是函数的调用处使用) def func(a,b,c): print(a,b,c) # 把列表当中的每一个元素,一个一个拿出来作为func的参数进行赋值; listvar = [1,2,3] func(*listvar) def func(a=1,b=2,c=3): print(a,b,c) dic = {‘a‘:10,"b":12,"c":13} func(**dic) # a=10,b=12,c=13 ‘‘‘
标签:优化 int return range 过程 res 版本 复习 pytho
原文地址:https://www.cnblogs.com/huangjiangyong/p/10920445.html