Mon*_*ath 5 python algorithm list
我有两组数据取自两个单独的导入文件,它们都被导入到 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])
Run Code Online (Sandbox Code Playgroud)
0(n)您可以通过迭代列表并将坐标存储在 a 中来降低复杂性,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])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10333 次 |
| 最近记录: |