2014-10-10 67 views
0

我有以下共20行數據如果循環替換值

1 2 5 4 1 
2 2 2 3 3 
3 3 4 1 2 
4 3 5 1 2 
5 4 3 8 4 
.... 

我希望能夠存儲每個列,並且要替換特定如果環條件一些值,並寫入行號,某些列中出現了多少個替換值。我寫了這樣的代碼

n_lines = 20 
A = [None] * n_lines 
B = [None] * n_lines 
C = [None] * n_lines 
D = [None] * n_lines 
E = [None] * n_lines 
with open ('output.txt', 'w') as outfile: 
    for i in range(n_lines):  ### Read everything to data_lines 
     data_lines[i] = file.readline() 
    for j in range(n_lines):  ### Read and store column by column 
     data = data_lines[j].split() 
     A[j] = int(data[0]) 
     B[j] = int(data[1]) 
     C[j] = int(data[2]) 
     D[j] = int(data[3]) 
     E[j] = int(data[4]) 
    for k in range(n_lines):  ### Analyze table 
     if B[k] == 2:    ### Check if 2nd column's value is 2 
      c1 = C[k]    ### If it is, read 3rd column and 4th column, store them as c1 and d1. 
      d1 = D[k] 
      if ((B[c1] == 4) and (B[d1] == 4)):  ### Check if 2nd column's c1-th and d1-th values are 4 
      B[k] = 9   ### If those conditions are met, replace B[k] value from 2 to 9  
      elif ((D[c1] + E[d1] >= 10)): 
      B[k] = 10   #### If this condition is met, replace B[k] value from 2 to 10 
    num_9 = [B[k]].count(9)  ### Count the occurrence number of replaced value 9 
    num_10 = [B[k]].count(10)  ### Count the occurrence number of replaced value 10 
    out = '%5d'%k + '%5d'%num_9 + '%5d'%num_10  ### Print out 
    outfile.write(out) 
    outfile.write('\n') 
outfile.close() 

但我面對

if ((B[c1] == 4) and (B[d1] == 4)): 
IndexError: list index out of range 

我不明白爲什麼「超出範圍」錯誤發生。 'elif((D [c1] + E [d1]> = 10)):'line也發生了同樣的錯誤。所有列(A〜E)大小合適。我的方式if-loop表達式是錯誤的?或者我的替換方式是錯誤的? (我在同一格式的其它數據是在15000線100數據blcoks,所以希望使用用於環路用於索引保持。)

謝謝

+0

你有數字矩陣,可以考慮在數據結構存儲他們像列表中,列表或'numpy.ndarray'。 – 2014-10-10 02:28:03

+0

@BrianCain謝謝,但我沒有索引和訪問列表和數組形式列表中的每個元素(如http://stackoverflow.com/questions/26208434/multidimension-array-indexing-and-column-accessing),所以我決定使用這種形式。至少我對這裏的索引,訪問和分割/讀取命令感到更加舒適。 – exsonic01 2014-10-10 02:31:17

回答

2

只有20 B中的元素根據[]到你的代碼。這個錯誤意味着你想訪問索引超出範圍的元素。您可以通過以下方式進行確認:

... 
for k in range(n_lines):  ### Analyze table 
     if B[k] == 2:    ### Check if 2nd column's value is 2 
      c1 = C[k]    ### If it is, read 3rd column and 4th column, store them as c1 and d1. 
      d1 = D[k] 
      print 'B[] has only 20 elements, now I try to visit %dth and %dth element.' % (c1, d1) 
      if ((B[c1] == 4) and (B[d1] == 4)):  ### Check if 2nd column's c1-th and d1-th values are 4 
      B[k] = 9   ### If those conditions are met, replace B[k] value from 2 to 9  
      elif ((D[c1] + E[d1] >= 10)): 
      B[k] = 10   #### If this condition is met, replace B[k] value from 2 to 10 
... 

然後您會知道您出錯的位置。希望能幫助到你。 :)

編輯:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

n_lines = 20 
A = [] 
B = [] 
C = [] 
D = [] 
E = [] 
with open ('output.txt', 'w') as outfile: 
    for line in file.readlines(): 
     data = line.split() 

     A.append(int(data[0])) 
     B.append(int(data[1])) 
     C.append(int(data[2])) 
     D.append(int(data[3])) 
     E.append(int(data[4])) 
    for k in xrange(n_lines):  ### Analyze table 
     if B[k] == 2:    ### Check if 2nd column's value is 2 
      c1 = C[k]    ### If it is, read 3rd column and 4th column, store them as c1 and d1. 
      d1 = D[k] 
      if ((B[c1] == 4) and (B[d1] == 4)):  ### Check if 2nd column's c1-th and d1-th values are 4 
       B[k] = 9   ### If those conditions are met, replace B[k] value from 2 to 9  
      elif ((D[c1] + E[d1] >= 10)): 
       B[k] = 10   #### If this condition is met, replace B[k] value from 2 to 10 
    num_9 = B.count(9)  ### Count the occurrence number of replaced value 9 
    num_10 = B.count(10)  ### Count the occurrence number of replaced value 10 
    out = ''.join(['%5d'%k, '%5d'%num_9, '%5d'%num_10]) 
    outfile.write(out) 
    outfile.write('\n') 
+0

謝謝,我發現問題並解決了。沒有更多的錯誤。但是,結果仍然遠離我的期望。你認爲我的替換方式在這裏很好嗎? – exsonic01 2014-10-10 18:56:02

+0

@ exsonic01你的代碼是可讀的,但不是pythonic。這對工作很好,但不夠好。所以,如果我的答案有幫助,不要忘記接受它作爲答案。 :) – 2014-10-11 01:26:56

+0

我的代碼是什麼pythonic的方式?我試過但保持未能索引和訪問多維數組或列表中的每個元素。(http://stackoverflow.com/questions/26208434/multidimension-array-indexing-and-column-accessing) 而且它似乎count()不適用於我的列。任何建議?謝謝 – exsonic01 2014-10-11 01:32:09