学习笔记(03):21天通关Python(仅视频课)-案例实操:定义计算矩阵转置的函数...

立即学习:https://edu.csdn.net/course/play/24797/282184?utm_source=blogtoedu

定义计算矩阵转置的函数

import numpy

testval = [[1, 2, 3, 4, 5, ], [6, 7, 8, 9, 10], ['a', 'b', 'c', 'd', 'e']]


# for示例----------------------------------
def testfor(val):
    for ele in val:
        for i in ele:
            print(i, end=" ")
        print(" ")


testfor(testval)


# for 转置
def zhuanzhi(val):
    newtv = [[] for i in val[0]]
    for ele in val:
        for i in range(len(ele)):
            newtv[i].append(ele[i])
    return newtv


print('-' * 20)
testfor(zhuanzhi(testval))


# zip 转置---------------------------------
def zipzhuanzhi(val):
    return list(zip(*val))

    print('-' * 20)
    testfor(zipzhuanzhi(testval))

    # numpy 转置-------------------------------


def numpyzhuanzhi(val):
    return numpy.transpose(val).tolist()


print('-' * 20)
testfor(numpyzhuanzhi(testval))
发布了25 篇原创文章 · 获赞 4 · 访问量 612

猜你喜欢

转载自blog.csdn.net/happyk213/article/details/105126330