嵌套的列表推导式(可以转置行列)学习

嵌套的列表推导式(可以转置行列)学习_第1张图片
代码练习

list1=[
    [1,2,3,4,5],
    [6,7,8,9,10],
    [11,12,13,14,15]
]
print(list1)
result1=[[list2[i] for list2 in list1] for i in range(len(list1[0]))]
print(result1)
result2=[[list3[i] for list3 in result1] for i in range(len(result1[0]))]
print(result2)
'''zip-test'''
print(list(zip(*result2)))
# print(list(zip(result2)))

运行结果

E:\Python\Python38\python.exe D:/pythonprojects/python-auto-test/test/qiantaoliebiaotuidaoshi.py
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
[[1, 6, 11], [2, 7, 12], [3, 8, 13], [4, 9, 14], [5, 10, 15]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
[(1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14), (5, 10, 15)]

Process finished with exit code 0

你可能感兴趣的:(python)