python坐标_比较在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.

List 1 is in the form:

(reference number, x coordinate, y coordinate)

Example list 1: [[1, 0, 0], [2, 0, 10], [3, 0, 20], [4, 0, 30], [5, 0, 40]]

List 2 is in the form:

(x coordinate, y coordinate, temperature)

Example list 2: [[0, 0, 100], [0, 10, 110], [0, 20, 120], [0, 30, 130], [0, 40, 140]]

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:

(reference number, temperature)

Example Output list: [[1, 100], [2, 110], [3, 120], [4, 130], [5, 140]]

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

解决方案

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])

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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值