2015-11-22 33 views
0

我試圖做一個矩陣進行陣列蟒蛇使用數組做一個矩陣

假設我的輸入是兩個數組

 my_column = [['a','2'] ['k','34','2'] ['d','e','5']] 
     my_row = ['a' '2' 'k' '34' 'd' 'e' '5'] 

我想這樣

   #a #2  #k  #34 #d #e #5 
     #a,2 1  1  0  0  0  0  0 
    #k,34,2 0  1  1  1  0  0  0 
    #d,e,5 0  0  0  0  1  1  1 

輸出但是,我在2D列表中表示此輸出。我期望的輸出是這

 output_matrix = [['1', '1', '0', '0', '0', '0', '0'],['0', '1', '1', '1', '0', '0', '0'],['0', '0', '0', '0', '1', '1', '1']] 

我匹配我的行和列,如果匹配得到1組成的矩陣,否則爲0。 #stuff只是一個更好理解的評論。

 output_matrix = [[]] 
     for i in output_matrix: 
      for j in i: 
       if my_row[i] == my_column[i][j]: 
       output_matrix.append(1) 
       else: 
        output_matrix.append(0) 

我試圖創建that.However一個新的二維表和存儲值,我的整個做法似乎是我得到的輸出是錯誤的,因爲只是[ [ ] ]

+1

我強烈建議尋找Python中的數字庫,如NumPy。使用NumPy矩陣比創建自己的矩陣要容易得多。此外,這些操作都在C級別,並且會更快。 –

+0

我更喜歡使用2D列表和數組,因爲我覺得它更容易。 – Kristy

回答

0

給你提供的輸入(I必須在列表中的項目),並且要輸出之間添加逗號,這樣做的伎倆:

my_column = [['a','2'], ['k','34','2'], ['d','e','5']] 
my_row = ['a', '2', 'k', '34', 'd', 'e', '5'] 

output_matrix = [] 
for r in my_column: 
    print "r=",r 
    output_matrix.append([]) 
    for c in my_row: 
     print "c=",c 
     if c in r: 
      output_matrix[-1].append(1) 
     else: 
      output_matrix[-1].append(0) 
print output_matrix 

輸出是:

[[1, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1]] 

順便說一句我會說你的名字my_column和my_row是錯誤的方式 - 即列應該是內部列表,行應該是外部列表。

這樣做的另一種方法是首先創建一個所有值爲0的必要大小的數組(列表),然後寫入匹配的1。

# construct a matrix with as many columns as my_row and as many rows as my_column 
output_matrix1 = [[0 for c in my_row] for r in my_column] 
for x,colvalue in enumerate(my_row): 
    for y, rowvalue in enumerate(my_column): 
     if colvalue in rowvalue: 
      output_matrix1[y][x] = 1 
print output_matrix1 
0
my_column = [['a','2'],['k','34','2'],['d','e','5']] 
my_row = ['a', '2', 'k', '34', 'd', 'e', '5'] 

output = [] 
for elem in my_column: 
    output_row = [] 
    for val in my_row: 
     if val in elem: 
      output_row.append(1) 
     else: 
      output_row.append(0) 
    output.append(output_row) 
print output 

這將打印出:

[[1, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1]] 
+0

請注意,如果您執行以下操作,兩個(相同)解決方案將不起作用:my_column = [['a','2'],['k','34','2'],['d',' e','5'],'d']你必須這樣做:my_column = [['a','2'],['k','34','2'],['d','e 」, '5'],[ 'd']] – PanGalactic

1

這將使用numpy的...

import numpy as np 
my_column = [['a','2'], ['k','34','2'], ['d','e','5']] 
my_row = ['a', '2', 'k', '34', 'd', 'e', '5'] 

results = np.zeros((len(my_column), len(my_row))) 

for i in range(0, len(my_column)): 
    for j in my_column[i]: 
     for ind, x in enumerate(my_row): 
      if x == j: 
       results[i,ind] = 1 

results 

array([[ 1., 1., 0., 0., 0., 0., 0.], 
     [ 0., 1., 1., 1., 0., 0., 0.], 
     [ 0., 0., 0., 0., 1., 1., 1.]]) 
0

你的默認輸出是絕對正確的。如果你想具體的輸出,你可以編寫自己喜歡:

# Cosmetic variables 
padding = 8 
truncating = 6 

# Data prepared for printing. 
my_column = [truncating*'-'] + ['a', '2', 'k', '34', 'd', 'e', '5'] 
my_row = [['a', '2'],[ 'k', '34', '2'],['d', 'e', '5']] 
output_matrix = [['1', '1', '0', '0', '0', '0', '0'],['0', '1', '1', '1', '0', '0', '0'],['0', '0', '0', '0', '1', '1', '1']] 


for element in my_column: 
    print('#{:{}.{}}'.format(''.join(element), padding, truncating), end="") 
else: 
    print() 

for index, list in enumerate(output_matrix): 
    print('#{:{}.{}}'.format(','.join(my_row[index]), padding, truncating), end="") 

    for element in list: 
     print(' {:{}.{}}'.format(''.join(element), padding, truncating), end="") 
    else: 
     print() 

Python 3.x都有

結果是接近你的願望:

#------ #a  #2  #k  #34  #d  #e  #5 
#a,2  1  1  0  0  0  0  0 
#k,34,2 0  1  1  1  0  0  0 
#d,e,5 0  0  0  0  1  1  1 

你怎麼能做到這一點通過自我?

Website: Using % & .format()

Documentation: Format String Syntax

歡呼!