第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

自己制作的矩陣函數(shù)打印錯(cuò)誤的項(xiàng)目位置

自己制作的矩陣函數(shù)打印錯(cuò)誤的項(xiàng)目位置

嗶嗶one 2023-03-22 17:12:00
我創(chuàng)建了一個(gè)函數(shù),它看起來(lái)像是一個(gè)矩陣,但是當(dāng)打印出來(lái)時(shí),它沒(méi)有打印出矩陣中項(xiàng)目的正確位置。然而,用于顯示當(dāng)前所在行和列的打印功能確實(shí)打印了正確的值,但附加的這些值卻沒(méi)有。而不是打印:[00, 01, 02][10, 11, 12][20, 21, 22]它打?。篬20, 21, 22][20, 21, 22][20, 21, 22]我設(shè)法意識(shí)到它實(shí)際打印的是:[x0, x1, x2][x0, x1, x2][x0, x1, x2]其中 (x = rows - 1) 而不是它應(yīng)該的當(dāng)前行。我制作矩陣的腳本是:rows = 3cols = 3matrix = []def makeMatrix(rows, cols):    curRow = []    for row in range(rows):        curRow.clear()        print("Row: ", row)        for col in range(cols):            print("Col: ", col)            toAppend = str(row) + str(col)            curRow.append(toAppend)        matrix.append(curRow)    printMatrix()def printMatrix():    for item in range(len(matrix)):        print(matrix[item])makeMatrix(rows, cols)
查看完整描述

3 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

您將覆蓋您的curRow3 次,然后最后一個(gè)值將是該變量的值。如果你不想要這種行為,你需要像這樣克隆你的列表:


rows = 3

cols = 3


matrix = []



def makeMatrix(rows, cols):

    curRow = []


    for row in range(rows):

        curRow.clear()

        print("Row: ", row)


        for col in range(cols):

            print("Col: ", col)

            toAppend = str(row) + str(col)

            curRow.append(toAppend)


        matrix.append(list.copy(curRow)) #Make a clone


    printMatrix()



def printMatrix():

    for item in range(len(matrix)):

        print(matrix[item])



makeMatrix(rows, cols)


查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊

由于嵌套 for ,您正在覆蓋行。這就是為什么總是采用最新的數(shù)字。你可以這樣解決這個(gè)問(wèn)題:


rows = 3

cols = 3


matrix = []


def make_matrix(rows, cols):

    for row in range(rows):

        curRow = []

        print("Row: ", row)


        for col in range(cols):

            print("Col: ", col)

            toAppend = str(row) + str(col)

            curRow.append(toAppend)


        matrix.append(curRow)


    print_matrix()



def print_matrix():

    for item in range(len(matrix)):

        print(matrix[item])


make_matrix(rows, cols)

我希望這有幫助。此外,我按照 PEP8 風(fēng)格為您的函數(shù)提供了更好的命名。


查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您將行替換curRow.clear()為curRow = []您將獲得所需的輸出,如下所示:


>>> 

('Row: ', 0)

('Col: ', 0)

('Col: ', 1)

('Col: ', 2)

('Row: ', 1)

('Col: ', 0)

('Col: ', 1)

('Col: ', 2)

('Row: ', 2)

('Col: ', 0)

('Col: ', 1)

('Col: ', 2)

['00', '01', '02']

['10', '11', '12']

['20', '21', '22']

這是在.下測(cè)試的Python 2.7。


Python 3.5在我得到相同結(jié)果的情況下實(shí)際測(cè)試您的原始代碼:


In [21]: makeMatrix(rows, cols)

Row:  0

Col:  0

Col:  1

Col:  2

Row:  1

Col:  0

Col:  1

Col:  2

Row:  2

Col:  0

Col:  1

Col:  2

['00', '01', '02']

['10', '11', '12']

['20', '21', '22']


查看完整回答
反對(duì) 回復(fù) 2023-03-22
  • 3 回答
  • 0 關(guān)注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)