2015-11-22 40 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

欢呼!