我有两个对象列表。每个列表都已按日期时间类型的对象的属性排序。我想将这两个列表组合成一个排序列表。最好的方法只是进行排序还是在 Python 中有更聪明的方法?
最佳答案
is there a smarter way to do this in Python
这还没有被提及,所以我会继续 - 有一个 merge stdlib function在 python 2.6+ 的 heapq 模块中。如果您要做的只是完成工作,那么这可能是一个更好的主意。当然,如果你想实现自己的,merge-sort的merge是要走的路。
>>> list1 = [1, 5, 8, 10, 50]
>>> list2 = [3, 4, 29, 41, 45, 49]
>>> from heapq import merge
>>> list(merge(list1, list2))
[1, 3, 4, 5, 8, 10, 29, 41, 45, 49, 50]
这里是 the documentation .
关于python - 在 Python 中组合两个排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/464342/