比较python中的两个坐标列表并使用坐标值分配值
我有两个分别从两个单独的导入文件中提取的数据集,这两个文件都被导入到python中,并且当前被放在列表中,如下所示。
清单1的格式为:
(参考编号,x坐标,y坐标)
示例列表1:[[1,0,0],[2,0,10],[3,0,20],[4,0,30],[5,0,40]]
清单2的形式为:
(x坐标,y坐标,温度)
示例列表2:[[0,0,100],[0,10,110],[0,20,120],[0,30,130],[0,40,140]]
我需要使用x和y坐标比较两个列表,如果找到匹配项,则会生成一个包含相应参考编号和温度的新列表。
例如,在输出列表上方的两个列表中,格式如下:
(参考编号,温度)
示例输出列表:[[1,100],[2,110],[3,120],[4,130],[5,140]]
这需要处理大量数据,我真的很难找到解决方案,我们将不胜感激。干杯
-
这行得通,
0(n^2)
但很容易阅读和理解。result = [] for reference, x, y in list1: for a, b, temperature in list2: if x == a and y == b: result.append([temperature, reference])
您可以
0(n)
通过遍历列表并将坐标存储为来降低复杂度,dict
如下所示:dict1 = {} for reference, x, y in list1: dict[(x, y)] = reference dict2 = {} for x, y, temperature in list2: dict2[(x, y)] = temperature result = [] for coordinate, reference in dict1.iteritems(): temperature = dict2.get(coordinate) if temperature: result.append([temperature, reference])