问题描述
我有两套,两者都被导入到Python和当前已被放置在列表如下两个单独导入文件获取的数据的。
I have two sets of data taken from two separate import files which are both being imported into python and have currently been placed in lists as follows.
表1的格式为:
(参考号,x坐标,y坐标)
实施例列表1:[[1,0,0],[2,0,10],[3,0,20],[4,0,30],[5,0,40]]
Example list 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]]
Example list 2: [[0, 0, 100], [0, 10, 110], [0, 20, 120], [0, 30, 130], [0, 40, 140]]
我需要使用x和y坐标来比较两个列表,如果他们找到一个匹配产生含有相应的参考号码和温度的新列表。
I need to compare the two lists using the x and y coordinates and if they find a match produce a new list containing the corresponding reference number and temperature.
例如从上面的输出列表中两个名单将遵循以下形式:
for example from the two lists above the output list would follow the form:
(参考号,温度)
实施例输出列表:[[1,100],[2,110],[3,120],[4,130],[5,140]
Example Output list: [[1, 100], [2, 110], [3, 120], [4, 130], [5, 140]]
这是要与大数据量的完成,我真的在努力寻找解决办法,任何帮助将是非常美联社preciated。干杯
This is to be done with a large amount of data and I am really struggling to find a solution, any help would be really appreciated. Cheers
推荐答案
本作品 0(N ^ 2)
但它是非常容易阅读和理解。
This works 0(n^2)
but it is very easy to read and understand.
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)
通过循环列表和在商店负责协调字典
如下:
You can reduce the complexity to 0(n)
by iterating over the lists and store coordinates in a dict
as follows:
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])
这篇关于比较在python坐标两个列表,并使用坐标值赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!