从二维列表中提取值以对其进行操作

我试图从二维列表中提取值,以保存它们并对其执行操作,原则上我从3行145、154和157中获取值,但是问题是我需要遍历整个列表值并提取接下来的3行,即每145行一行,每154行另一行,每157行另一行,因此直到完成阅读整个列表为止。

file = open('path.dat', 'r')

listA = []
for line in file.readlines():
    a = line.split()
    listA.append(a)

Example out listA:

listA = [
     ['5', '0.0000', '0.9992', '0.9999', '0.9999'],
     ['6', '0.0000', '0.9986', '0.9999', '0.9999'],
     ['7', '0.0000', '0.9977', '0.9997', '0.9999'],
     ['8', '0.0000', '0.9961', '0.9992', '0.9998'],
     ['9', '0.0000', '0.9945', '0.9987', '0.9997'],
     ['10', '0.0000', '0.9928', '0.9980', '0.9995'],
     ['11', '0.0000', '0.9914', '0.9975', '0.9994'],
     ['12', '0.0000', '0.9901', '0.9972', '0.9992'],
     ['13', '0.0000', '0.9889', '0.9968', '0.9990'],
     ['14', '0.0000', '0.9877', '0.9965', '0.9988'],
     ['15', '0.0000', '0.9864', '0.9962', '0.9986'],
     ['16', '0.0000', '0.9851', '0.9959', '0.9984'],
     ['17', '0.0000', '0.9836', '0.9956', '0.9982'],
     ['18', '0.0000', '0.9813', '0.9950', '0.9978'],
     ['19', '0.0000', '0.9783', '0.9943', '0.9973'],
     ['20', '0.0000', '0.9751', '0.9936', '0.9967'],
     ['21', '0.0000', '0.9713', '0.9927', '0.9959'],
     ['22', '0.0000', '0.9673', '0.9917', '0.9949'],
     ['23', '0.0000', '0.9634', '0.9906', '0.9939'],
     ['24', '0.0000', '0.9586', '0.9894', '0.9925'],
     ['25', '0.0000', '0.9517', '0.9876', '0.9904'],
     ['26', '0.0000', '0.9408', '0.9847', '0.9866'],
     ['27', '0.0000', '0.9280', '0.9806', '0.9811'],
     ['28', '0.0000', '0.9178', '0.9769', '0.9760'],
     ['29', '0.0000', '0.9117', '0.9745', '0.9727'],
     ['30', '0.0000', '0.9022', '0.9706', '0.9673'],
     ['31', '0.0000', '0.8916', '0.9656', '0.9603'],
     ['32', '0.0000', '0.8831', '0.9610', '0.9540'],
     ['33', '0.0000', '0.8696', '0.9532', '0.9433']
]

With this I read a .dat or .txt file, with 60,000 lines and save it in a two-dimensional list. To extract the values that I need I have tried the following:

for i in listA:    
    line_A = listA[145] # Here I declare that I want to extract line 145
    line_B = listA[154] # Here I declare that I want to extract line 154
    line_C = listA[157] # Here I declare that I want to extract line 157

在这里,我将字符串传递给float类型,以便能够对其进行操作:

totalA = float(line_A[1]), float(line_A[2]), float(line_A[3])
totalB = float(line_B[1]), float(line_B[2]), float(line_B[3])
totalC = float(line_C[1]), float(line_C[2]), float(line_C[3])

print(totalA)
print(totalB)
print(totalC)

(48.47, 89.82, 101.94)
(48.37, 89.72, 101.79)
(6.17, 5.56, 8.49)

file.close()

原则上,我得到了我需要的前三行,但是我无法让它遍历整个列表。

评论