Python 嵌套列表系列中出现数字错误
Python Numerical Error Arising in Series of Nested Lists
我正在尝试编写一个脚本来解析一个包含位置和时间信息的非常大的数据文件,并将该信息存储在一个数组(即列表列表)中,但是,出于某种原因,我的代码是在多个子列表中写入相同的数字。
# Import data from inputFile into list
with open(r"C:\..file.dat") as inputFile:
inputList = list(inputFile)
totalSegs = 775
totalPrds = 938
stressPrd = 1
segNum = 1
testList = []
masterList = []
while stressPrd <= totalPrds: #build a list of times
testList.append(0)
stressPrd += 1
while segNum <= totalSegs: #build a list of locations
masterList.append(testList) #list of time periods by location
segNum += 1
stressPrd = 1
segNum = 1
for inputItem in inputList: #read data from file and write to lists
if inputItem != '\n'
inputItem = inputItem.split()
if int(inputItem[3]) == int(segNum):
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
else:
segNum += 1
if segNum <= totalSegs:
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
else:
segNum = 1
stressPrd += 1
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
应该发生的是同一时间 (stressPrd) 的同一位置 (segNum) 的数据被求和并存储在由 stressPrd 组织的子列表中,然后给定位置跨时间的列表由父列表中的 segNum。然而,正在发生的事情是,脚本改为对给定时间 (stressPrd) 内所有位置的所有数据求和,并且该总和存储在每个位置子列表中。
我试过插入一些打印语句来同时跟踪多个子列表发生的事情,它们都同时开始计算相同的总和。我的变量 segNum 和 stressPrd 似乎都正确递增,而且我可以告诉我 If 语句正在正确执行,所以我无法找出问题的根源。
作为我想要的结果的一个非常简化的例子:
Data:
Loc Time1 Time2
A 6 1
A 2 2
B 2 3
C 5 4
C 1 1
Result:
[[8,3],[2,3],[6,5]]
提前致谢!
如果您正在使用表格数据,我强烈建议您切换到 pandas,而不是尝试调试此处出现的特定问题。这是一个非常简单的问题:
In [16]: import pandas as pd
In [17]: from StringIO import StringIO
In [18]: datatable = """Loc Time1 Time2
A 6 1
A 2 2
B 2 3
C 5 4
C 1 1"""
In [19]: df = pd.read_csv(StringIO(datatable), sep=" +", engine="python")
In [20]: df.groupby("Loc").sum()
Out[20]:
Time1 Time2
Loc
A 8 3
B 2 3
C 6 5
如果您想要指定的特定格式,也可以很容易地在最后提取它:
In [28]: [list(v[1].values) for v in df.groupby("Loc").sum().iterrows()]
Out[28]: [[8, 3], [2, 3], [6, 5]]
我正在尝试编写一个脚本来解析一个包含位置和时间信息的非常大的数据文件,并将该信息存储在一个数组(即列表列表)中,但是,出于某种原因,我的代码是在多个子列表中写入相同的数字。
# Import data from inputFile into list
with open(r"C:\..file.dat") as inputFile:
inputList = list(inputFile)
totalSegs = 775
totalPrds = 938
stressPrd = 1
segNum = 1
testList = []
masterList = []
while stressPrd <= totalPrds: #build a list of times
testList.append(0)
stressPrd += 1
while segNum <= totalSegs: #build a list of locations
masterList.append(testList) #list of time periods by location
segNum += 1
stressPrd = 1
segNum = 1
for inputItem in inputList: #read data from file and write to lists
if inputItem != '\n'
inputItem = inputItem.split()
if int(inputItem[3]) == int(segNum):
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
else:
segNum += 1
if segNum <= totalSegs:
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
else:
segNum = 1
stressPrd += 1
testVar = float(masterList[segNum - 1][stressPrd - 1])
testVar += float(inputItem[6])
masterList[segNum - 1][stressPrd - 1] = testVar
应该发生的是同一时间 (stressPrd) 的同一位置 (segNum) 的数据被求和并存储在由 stressPrd 组织的子列表中,然后给定位置跨时间的列表由父列表中的 segNum。然而,正在发生的事情是,脚本改为对给定时间 (stressPrd) 内所有位置的所有数据求和,并且该总和存储在每个位置子列表中。
我试过插入一些打印语句来同时跟踪多个子列表发生的事情,它们都同时开始计算相同的总和。我的变量 segNum 和 stressPrd 似乎都正确递增,而且我可以告诉我 If 语句正在正确执行,所以我无法找出问题的根源。
作为我想要的结果的一个非常简化的例子:
Data:
Loc Time1 Time2
A 6 1
A 2 2
B 2 3
C 5 4
C 1 1
Result:
[[8,3],[2,3],[6,5]]
提前致谢!
如果您正在使用表格数据,我强烈建议您切换到 pandas,而不是尝试调试此处出现的特定问题。这是一个非常简单的问题:
In [16]: import pandas as pd
In [17]: from StringIO import StringIO
In [18]: datatable = """Loc Time1 Time2
A 6 1
A 2 2
B 2 3
C 5 4
C 1 1"""
In [19]: df = pd.read_csv(StringIO(datatable), sep=" +", engine="python")
In [20]: df.groupby("Loc").sum()
Out[20]:
Time1 Time2
Loc
A 8 3
B 2 3
C 6 5
如果您想要指定的特定格式,也可以很容易地在最后提取它:
In [28]: [list(v[1].values) for v in df.groupby("Loc").sum().iterrows()]
Out[28]: [[8, 3], [2, 3], [6, 5]]