我有以下共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,所以希望使用用于环路用于索引保持。)
谢谢
你有数字矩阵,可以考虑在数据结构存储他们像列表中,列表或'numpy.ndarray'。 – 2014-10-10 02:28:03
@BrianCain谢谢,但我没有索引和访问列表和数组形式列表中的每个元素(如http://stackoverflow.com/questions/26208434/multidimension-array-indexing-and-column-accessing),所以我决定使用这种形式。至少我对这里的索引,访问和分割/读取命令感到更加舒适。 – exsonic01 2014-10-10 02:31:17