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)
字典推导式:
原文地址:https://www.cnblogs.com/xyg-zyx/p/9795258.html
时间: 2024-11-09 05:56:05