2012-10-21 42 views
6

下面是我的腳本,它基本上創建了一個填充0的12x8的零矩陣。然後我想逐個填充它。因此,讓我們說列2行0需要是5.我該怎麼做?下面的例子顯示瞭如何做到了,錯了(我需要)輸出:Python中二維矩陣的單元格分配,無numpy

list_MatrixRow = [] 
list_Matrix = [] #Not to be confused by what the book calls, optimal alignment score matrix 

int_NumbOfColumns = 12 
int_NumbOfRows = 8 

for i in range (0, int_NumbOfColumns): # Puts Zeros across the first Row 
    list_AlignMatrixRow.append(0) 
for i in range (0, int_NumbOfRows): 
    list_AlignMatrix.append(list_AlignMatrixRow) 
#add the list in another list to make matrix of Zeros 
#-------------------THE ACTUAL PROBLEMATIC PART; ABOVE IS FINE(It Works)------------ 

list_AlignMatrix[2][0] = 5 
# This is what logically makes sense but here is the output 
# which happens but I don't want (there should be all 0s and 
# only one 5 on the cell [2][0]): 

[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
+0

THX大家!我知道了。我正在做的是製作一張列表,列表中的所有列表都是指向同一列表的列表。也感謝你在Python中編寫正確的方式。我是新的(4周)使用python。謝謝大家! – StudentOfScience

回答

8

每行指向相同的子列表。這是重複追加同一個子列表的結果。所以當你修改一行時,你最終會修改其他的。

我這樣做:

ncols = 12 
nrows = 8 
matrix = [[0] * ncols for i in range(nrows)] 
matrix[2][0] = 5 

matrix包含:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 

旁白關於編碼風格:它是在Python拙劣的形式,包括其名稱的對象的類型。我選擇將int_NumbOfColumns重命名爲ncols。如果你需要更多描述性的東西,比如column_count。一般來說,要避免使用混合類名稱,而CamelCase通常用於類名稱。有關更多信息,請參閱PEP 8 -- Style Guide for Python Code

編輯:既然你提到你是Python的新手,下面再來解釋一下。

這是一個list comprehension

matrix = [[0] * ncols for i in range(nrows)] 

它也可以寫成一個普通的for循環:

matrix = [] 
for i in range(nrows): 
    matrix.append([0] * ncols) 
+0

謝謝!有效!! – StudentOfScience

5

list_AlignMatrix每個條目是同一個對象參考。您需要爲矩陣中的每一行創建一個新的list實例。這裏是正在發生的事情的說明:

>>> l = [0] 
>>> l2 = [l,l,l] 
>>> l2 
[[0], [0], [0]] 
>>> l2[0][0] = 1 
>>> l2 
[[1], [1], [1]] 

可以使用id()功能,以確認在l2每個條目是對同一個對象的引用:

>>> [id(x) for x in l2] 
[161738316, 161738316, 161738316] 

爲了讓您排列表的新副本,你可以重寫你的第二個循環是這樣的:

for i in range (0, int_NumbOfRows): 
    list_AlignMatrix.append(list(list_AlignMatrixRow)) 

list構造函數將創建份,通過下面的例子所示:

>>> l = range(10) 
>>> l2 = list(l) 
>>> l == l2 
True 
>>> l is l2 
False 
+0

謝謝!我現在明白了! – StudentOfScience

1

當你追加list_AlignMatrixRow,它只是追加到原始列表的引用,所以沒什麼一個1-d清單,而矩陣的每一行是指向它。要創建一個新的列表,實際上你需要創建一個新的列表:

list_AlignMatrix.append(list(list_AlignMatrixRow)) 

注意調用列表,它通過迭代和複製的list_AlignMatrixRow

+0

是的!我是python上的newbee .. thankyoU! – StudentOfScience

1

的元素創建一個列表來生成這樣的矩陣在蟒蛇,你應該使用列表comprihention這樣 如果要生產出全0一排,

>>> import copy 
>>> list_MatrixRow=[0 for i in range(12)] 
>>> list_MatrixRow 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

CE 那麼您可以在同樣的方式

創建列表清單

list_Matrix = [[0爲在範圍(12)J]爲i的範圍(8)]

現在可以編輯的任何元件

>>> list_Matrix[0][2]=12345 
>>> list_Matrix[0][2] 
12345 
>>> list_Matrix 
[[0, 0, 12345, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 

如果你想創建矩陣包含所有列5,你可以使用列表理解短路評估

>>> list_MatrixRow=[(i==0 and 5 or 0) for i in range(12)] 
>>> list_MatrixRow 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
>>> list_Matrix=[list_MatrixRow for i in range(8)] 
>>> list_MatrixRow 
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
>>> list_Matrix 
[[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
>>> list_Matrix[0][0] 
5  
+0

任何人都可以告訴我爲什麼我的回答爲負面投票!我認爲它只是給出了他想要的結果 –

+0

你還應該提供一個簡短的解釋,說明你爲什麼需要這樣做(參見其他答案)。附:不是我的downvote。 – nneonneo

+1

我不低調,但我認爲很明顯爲什麼有人這麼做:OP正試圖*避免*讓每個矩陣行成爲同一行。看到他的註釋「這是輸出發生,但我不想要」。你可以重用'list_MatrixRow',這會讓你陷入OP遇到的同樣的問題。 – DSM