1. 程式人生 > >python 推導式

python 推導式

[] 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] for
i in range(3) for j in range(3)] print(sum1)

字典推導式:

技術分享圖片

python 推導式