Python列表推导式

A0 =dict(zip(('a', 'b', 'c', 'd', 'e'), (1, 2, 3, 4, 5)))

A1 =range(10)

A2 = [ifor iin A1if iin A0]

A3 = [A0[s]for sin A0]

A4 = [ifor iin A1if iin A3]

A5 = {i: i*ifor iin A1}

A6 = [[i, i*i]for iin A1]

print(A0)#{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

print(A1)#range(0, 10)

print(A2)#[]

print(A3)#[1, 2, 3, 4, 5]

print(A4)#[1, 2, 3, 4, 5]

print(A5)#{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

print(A6)#[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]

 

posted @ 2018-02-20 15:12  小学弟-  阅读(146)  评论(0编辑  收藏  举报