python 推导式
Posted 笨丫头就是笨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 推导式相关的知识,希望对你有一定的参考价值。
names = [\'Tom\',\'Billy\',\'Jefferson\',\'Andrew\',\'Wesley\',\'Steven\', \'Joe\',\'Alice\',\'Jill\',\'Ana\',\'Wendy\',\'Jennifer\',\'Sherry\',\'Eva\'] names1=[] # 声明空列表 names2=[] for i in names: if len(i)>4: names1.append(i) print(names1) for i in names: if len(i)<=5: names2.append(i.upper()) print(names2) #列推导式 names11 = [i for i in names if len(i)>4] names12 = [i.upper() for i in names if len(i)<=5] print(names11) print(names12)
m = [[1,2,3],[4,5,6],[7,8,9]] n = [[2,2,2],[3,3,3],[4,4,4]] for i in range(0,3): for j in range(0,3): print(m[i][j]+n[i][j]) #列推导式 sum1 =[m[i][j]+n[i][j] for i in range(3) for j in range(3)] print(sum1)
字典推导式:
以上是关于python 推导式的主要内容,如果未能解决你的问题,请参考以下文章