1、[(x,y) for x in [1,2,3] for y in [4,2,3] if x == y]
(x,y):输出表达式,产生最终列表的元素
for x in [1,2,3] for y in [4,2,3]:输入列表
if x == y:可选的判断表达式
m = [(x,y) for x in [1,2,3] for y in [4,2,3] if x == y] print(m) 控制台输出:[(2, 2), (3, 3)]
2、举个例子,矩阵转置
list1 = [[1,2,3],[4,5,6],[7,8,9]] #把它变为 [[1,4,7],[2,5,8],[3,6,9]] m = [[lis[i] for row in list1] for i in range(3)] #外层是一个列表,内层是一个列表,当i=0时,内层[lis[0] for lis in list1 ] 得到第一个内层列表[1,4,7] print(m) 控制台输出:[[1, 4, 7], [2, 5, 8], [3, 6, 9]]