2014-10-10 60 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