Study notes (03): 21 days the Python clearance (Only Video Class) - Case practical operation: calculation of matrix transpose defined function ...

Learning immediately: https://edu.csdn.net/course/play/24797/282184?utm_source=blogtoedu

Define calculated matrix transpose function

 

import numpy

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


# For example ----------------------------------
def test feed (val):
    for ele in val:
        for i in ele:
            print(i, end=" ")
        print(" ")


testfor (testval)


# For transposition
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 transpose ---------------------------------
def zipzhuanzhi(val):
    return list(zip(*val))

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

    # Numpy transpose -------------------------------


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


print('-' * 20)
testfor (numpyzhuanzhi (testval))
Published 25 original articles · won praise 4 · Views 612

Guess you like

Origin blog.csdn.net/happyk213/article/details/105126330