列表推导式

li=[[1,2,3],[2,3,4],[3,4,5],[4,5,6]]
xx=[[y*y for y in i] for i  in li]
aa=[y*y for i in li for y in i]
print(aa)#[1, 4, 9, 4, 9, 16, 9, 16, 25, 16, 25, 36]
print(xx)#[[1, 4, 9], [4, 9, 16], [9, 16, 25], [16, 25, 36]]

a=[{'name':'zhangsan','age':18},{'name':'lisi','age':20}]

d=[[value for value in item.values()] for item in a ]

print(d)#[['zhangsan', 18], ['lisi', 20]]

d=[[key for key in item.keys()] for item in a ]
print(d)#[['name', 'age'], ['name', 'age']]

 

posted @ 2019-06-22 15:46  青春叛逆者  阅读(129)  评论(0编辑  收藏  举报