python 推導式
阿新 • • 發佈:2018-10-16
[] inf err pen pytho 字典推導式 andrew print code
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] fori in range(3) for j in range(3)] print(sum1)
字典推導式:
python 推導式