在python中比较两个坐标列表并使用坐标值来分配值

在python中比较两个坐标列表并使用坐标值来分配值,第1张

在python中比较两个坐标列表并使用坐标值来分配值

这行得通,

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5615460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存