我一直在Python中使用cProfile
来更好地了解我应该集中精力减少计算时间。
偶尔我会遍历两个列表并找到匹配的对。一个列表包含整数列表,第二个列表是一个dicts列表。通常,一个列表很长,另一个列表很短。
考虑我有一个列表,nodes
:
[[532134.752, 181854.93], [532132.6, 181861.504]]
我还有一个列表,links
:
[{'index': 0, 'polyline': [523732, 189041, 523735, 189069]},
{'index': 1, 'polyline': [523711, 189069, 523732, 189041]},
{'index': 2,
'polyline': [499766.775,
151379.967,
499766,
151381,
499763,
151385,
499760,
151387,
499756,
151389,
499751,
151389,
499750,
151389]},
{'index': 3,
'polyline': [519679,
196844,
519683,
196842,
519688,
196838,
519699,
196827,
519702,
196822,
519705,
196818,
519707.203,
196814.758,
519708.274,
196810.043]},
{'index': 4,
'polyline': [499442,
152969,
499445,
152971,
499447,
152973,
499448.105,
152975.987,
499447,
152981]},
{'index': 5,
'polyline': [524647.824,
197808.45,
524645.358,
197803.279,
524643.13,
197798.823,
524640.346,
197791.027,
524639,
197786,
524635,
197760,
524634,
197755]},
{'index': 6,
'polyline': [499430.818,
152970.364,
499432.348,
152969.551,
499433.862,
152968.746,
499437,
152968,
499442,
152969]},
{'index': 7,
'polyline': [524647.824,
197808.45,
524648,
197797,
524643,
197772,
524641,
197754]},
{'index': 8,
'polyline': [518334.171,
198052.368,
518327,
198045,
518325,
198043,
518325,
198040,
518325,
198038,
518327,
198034,
518337,
198023]},
{'index': 9,
'polyline': [499770,
151368,
499770,
151373,
499769,
151377,
499766.775,
151379.967]}]
我想在link
列表中找到links
中的nodes
条记录。
localised_links = []
for node in nodes:
for link in links:
# the matching heuristic. Matches if the coordinates are first or last in polyline.
if node == link['polyline'][0:2] or node == link['polyline'][-2:]:
localised_links.append(link)
如何提高此类操作的速度(如果有的话)?