我有一个GPS坐标列表。我还具有一个比较两个GPS坐标并计算值的函数。
我知道我可以创建一个嵌套的循环来在每对上运行该功能,但这似乎不高。
是否有建议的方法可以在列表中的项目上运行比较功能?
谢谢。
您可以使用itertools.combinations:
>>> from itertools import combinations
>>> list(combinations([1,2,3,4,5],2))
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
这是可以的,因此您可以迭代并处理数据。
>>> for first, second in combinations([1,2,3,4,5],2):
... print first, second
... # perform your operation with first and second
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5