在 Python 2.7 中计算和存储函数输出?
Calculate and store function outputs in Python 2.7?
我有这样的代码:
nList = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]
import math
prevX, prevY, prevT = 0, 0, 0
#Entry point
for traceIndex in range(0, len(nList)):
print 'trace: ' + str(traceIndex+1)
trace = nList[traceIndex]
for pointIndex in range(0, len(trace)):
point = trace[pointIndex]
if len(point)>0:
tempX, tempY, tempT = point[0], point[1], point[2]
if pointIndex != 0:
#calculate time difference here
timeDiff = calculateTime (tempT,prevT)
基本上,nList
在每个 \
称为轨迹之前都有子列表,每个轨迹都有三个元素的点。例如,nList[0][0]
产生迹线 1,点 1= [0,0,0]
。 point=[x-coordinate, y-coordinate, time]
。我计算了每条轨迹中每个点的 timeDiff。现在我需要总结不同轨迹的 timeDiff 并打印它们以便:
trace: 1
623400
trace: 2
trace: 3
trace: 4
187393
nList 由名为 'trace' 的子列表组成,每个 'trace' 具有一个或多个点,其中包含 3 个元素 [x, y, t]。例如,trace1 有 2 个点,使得 trace1point1 = [0,0,0] 和 trace1point2=[100420,0,623400]。 timeDiff 计算 t2 和 t1 之间的差异。对于 trace1,这将是 (623400-0)。 trace4,与 trace 1 相比有更多的点,timeDiff 将用于具有 1=<N=<4
、(34300-543)、(7342-34300) 和 (134020-7342) 的单个 trace4pointN。我想编写一个程序,获取每个跟踪中的所有 timeDiffs,并以产生上述输出的方式对它们求和。
这更容易解决,使用 zip 并直接遍历元素以避免需要在变量中存储尽可能多的内容。根据您的示例输出,您需要每个时间点之间的绝对差异:
traces = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]]
TIME_INDEX = 2
traceCounter = 1
for trace in traces:
print "trace:", traceCounter
traceCounter += 1
if len(trace[0]) < 2:
#no coordinate in first element of trace, nothing to do
continue
#Zip takes several lists as arguments and returns list of lists with every 0th element in the 0th list, every 1st element in the 1st list etc.
timeStamps = zip(*trace)[TIME_INDEX]
sumOfTimeDiffs = sum([abs(y-x) for x, y in zip(timeStamps[:-1], timeStamps[1:])] )
if sumOfTimeDiffs > 0:
print sumOfTimeDiffs
输出:
trace: 1
623400
trace: 2
trace: 3
trace: 4
187393
nList = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]]
for trace in nList:
list1=list()
trace_index = nList.index(trace)
print "trace%d"%(trace_index+1)
if len(trace)>1:
for point in trace:
list1.append(point[2])
list2 = list1[1:]
list1.pop()
output = (abs(i2 - i1) for i2,i1 in zip(list2,list1))
print(sum(output))
这应该有效。基本上我通过提取每个轨迹点的时间来形成一个列表。然后形成了相同的重复列表。从一个列表中删除第一个元素,从另一个列表中删除最后一个元素。然后减去列表。在结果列表中添加元素给出输出。
我有这样的代码:
nList = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]
import math
prevX, prevY, prevT = 0, 0, 0
#Entry point
for traceIndex in range(0, len(nList)):
print 'trace: ' + str(traceIndex+1)
trace = nList[traceIndex]
for pointIndex in range(0, len(trace)):
point = trace[pointIndex]
if len(point)>0:
tempX, tempY, tempT = point[0], point[1], point[2]
if pointIndex != 0:
#calculate time difference here
timeDiff = calculateTime (tempT,prevT)
基本上,nList
在每个 \
称为轨迹之前都有子列表,每个轨迹都有三个元素的点。例如,nList[0][0]
产生迹线 1,点 1= [0,0,0]
。 point=[x-coordinate, y-coordinate, time]
。我计算了每条轨迹中每个点的 timeDiff。现在我需要总结不同轨迹的 timeDiff 并打印它们以便:
trace: 1
623400
trace: 2
trace: 3
trace: 4
187393
nList 由名为 'trace' 的子列表组成,每个 'trace' 具有一个或多个点,其中包含 3 个元素 [x, y, t]。例如,trace1 有 2 个点,使得 trace1point1 = [0,0,0] 和 trace1point2=[100420,0,623400]。 timeDiff 计算 t2 和 t1 之间的差异。对于 trace1,这将是 (623400-0)。 trace4,与 trace 1 相比有更多的点,timeDiff 将用于具有 1=<N=<4
、(34300-543)、(7342-34300) 和 (134020-7342) 的单个 trace4pointN。我想编写一个程序,获取每个跟踪中的所有 timeDiffs,并以产生上述输出的方式对它们求和。
这更容易解决,使用 zip 并直接遍历元素以避免需要在变量中存储尽可能多的内容。根据您的示例输出,您需要每个时间点之间的绝对差异:
traces = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]]
TIME_INDEX = 2
traceCounter = 1
for trace in traces:
print "trace:", traceCounter
traceCounter += 1
if len(trace[0]) < 2:
#no coordinate in first element of trace, nothing to do
continue
#Zip takes several lists as arguments and returns list of lists with every 0th element in the 0th list, every 1st element in the 1st list etc.
timeStamps = zip(*trace)[TIME_INDEX]
sumOfTimeDiffs = sum([abs(y-x) for x, y in zip(timeStamps[:-1], timeStamps[1:])] )
if sumOfTimeDiffs > 0:
print sumOfTimeDiffs
输出:
trace: 1
623400
trace: 2
trace: 3
trace: 4
187393
nList = [[[0,0,0],[100420,0,623400]],\
[[]],\
[[100043,1324000,123240]],\
[[0,0,543],[3002340,443000,34300],[334000,4043400,7342],[0,0,134020]]]
for trace in nList:
list1=list()
trace_index = nList.index(trace)
print "trace%d"%(trace_index+1)
if len(trace)>1:
for point in trace:
list1.append(point[2])
list2 = list1[1:]
list1.pop()
output = (abs(i2 - i1) for i2,i1 in zip(list2,list1))
print(sum(output))
这应该有效。基本上我通过提取每个轨迹点的时间来形成一个列表。然后形成了相同的重复列表。从一个列表中删除第一个元素,从另一个列表中删除最后一个元素。然后减去列表。在结果列表中添加元素给出输出。