if循环替换值

时间:2014-10-10 02:24:41

标签: python if-statement replace

我有以下总共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
....

我希望存储每个列,并希望替换特定if循环条件下的某些值,并写入行号,在某些列中出现了多少替换值。我写了一个像

这样的代码
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))中也发生相同的错误:'行也是如此。所有列(A~E)的大小都是正确的。我的if-loop表达式的方式是错误的?或者我的替换方式是错的? (我的其他相同格式的数据是15000行,有100个data-blcoks,所以我希望继续使用for循环进行索引。)

谢谢

1 个答案:

答案 0 :(得分:2)

根据您的代码,B []中只有20个元素。此错误意味着您要访问索引超出范围的元素。您可以通过以下方式确认:

...
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')