我有两个对象数组,我想按照属于每个对象一部分的时间戳顺序将它们组合成一个数组。第一个数组是精确的第二个数组,第二个数组是一个小时的范围。我想将精确的时间戳记在数组中的“湿度”值输入到每小时范围的值中,具体取决于它们在时间上适合的位置。我该如何在打字稿中这样做?这两个数组/对象都按时间排序。
例如:
[{id: 1, timestampUtc: "2019-02-22T08:24:00Z", humidity: 74},
{id: 2, timestampUtc: "2019-02-24T06:20:00Z", humidity: 39},
{id: 3, timestampUtc: "2019-02-26T020:03:00Z", humidity: 35}]
和
[{id: 4, starttimestampUtc: "2019-02-22T08:00:00Z", endtimestampUtc: "2019-02-22T09:00:00Z", precipitation: .03},
{id: 5, starttimestampUtc: "2019-02-24T06:00:00Z", endtimestampUtc: "2019-02-24T07:00:00Z", precipitation: .3},
{id: 6, starttimestampUtc: "2019-02-26T020:00:00Z",endtimestampUtc: "2019-02-26T021:00:00Z", precipitation: .12}]
外观如下:
[{id: 4, starttimestampUtc: "2019-02-22T08:00:00Z", endtimestampUtc: "2019-02-22T09:00:00Z", precipitation: .03, humidity: 74},
{id: 5, starttimestampUtc: "2019-02-24T06:00:00Z", endtimestampUtc: "2019-02-24T07:00:00Z", precipitation: .3, humidity: 39},
{id: 6, starttimestampUtc: "2019-02-26T020:00:00Z", endtimestampUtc: "2019-02-26T021:00:00Z", precipitation: .12, , humidity: 35}]
答案 0 :(得分:0)
我能够通过将UTC时间戳转换为日期对象来做到这一点。以下代码似乎对我有用,其中a和b是您的两个数组。我还注意到,每个数组的最后一个元素的时间戳中都有一个额外的0。
import argparse
def dijkstra(graph,src,dest,visited=[],distances={},predecessors={}):
# a few sanity checks
if src not in graph:
raise TypeError('The root of the shortest path tree cannot be found')
if dest not in graph:
raise TypeError('The target of the shortest path cannot be found')
# ending condition
if src == dest:
# We build the shortest path and display it
path=[]
pred=dest
while pred != None:
path.append(pred)
pred=predecessors.get(pred,None)
print('shortest path: '+str(path)+" cost="+str(distances[dest]))
else :
# if it is the initial run, initializes the cost
if not visited:
distances[src]=0
# visit the neighbors
for neighbor in graph[src] :
if neighbor not in visited:
new_distance = distances[src] + graph[src][neighbor]
if new_distance < distances.get(neighbor,float('inf')):
distances[neighbor] = new_distance
predecessors[neighbor] = src
# mark as visited
visited.append(src)
# now that all neighbors have been visited:
recurse
# select the non visited node with lowest distance 'x'
# run Dijskstra with src='x'
unvisited={}
for k in graph:
if k not in visited:
unvisited[k] = distances.get(k,float('inf'))
x=min(unvisited, key=unvisited.get)
dijkstra(graph,x,dest,visited,distances,predecessors)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('g', help='This is the network')
# Parse the given arguments
args = parser.parse_args()
graph = {'s': {'a': 2, 'b': 1}, 'a': {'s': 3, 'b': 4, 'c':8}, 'b': {'s': 4, 'a': 2, 'd': 2}, 'c': {'a': 2, 'd': 7, 't': 4}, 'd': {'b': 1, 'c': 11, 't': 5}, 't': {'c': 3, 'd': 5}}
#print args.g
#print graph
dijkstra(args.g,'s','t')