我有两个对象列表.每个列表已经按日期时间类型的对象的属性进行排序.我想将这两个列表合并为一个排序列表.是进行排序的最好方法还是有更智能的方法在Python中执行此操作?
这是一个看似简单的问题:给定一个按升序生成整数序列的迭代器列表,编写一个简洁的生成器,只生成每个序列中出现的整数.
在昨晚阅读了几篇论文之后,我决定在Python中破解一个完全最小的全文索引器,如此处所示(尽管该版本现在已经很老了).
我的问题在于search()函数,它必须迭代每个发布列表并仅产生每个列表上显示的文档ID.正如您从上面的链接中看到的那样,我当前的非递归"工作"尝试非常糟糕.
示例:
postings = [[1, 100, 142, 322, 12312],
[2, 100, 101, 322, 1221],
[100, 142, 322, 956, 1222]]
Run Code Online (Sandbox Code Playgroud)
应该产量:
[100, 322]
Run Code Online (Sandbox Code Playgroud)
至少有一个优雅的递归函数解决方案,但我想尽可能避免这种情况.但是,一个涉及嵌套生成器表达式,itertools滥用或任何其他类型的代码高尔夫的解决方案非常受欢迎.:-)
应该可以安排函数只需要与最小列表中的项目一样多的步骤,并且不将整个整数集吸入内存.将来,这些列表可能从磁盘读取,并且大于可用RAM.
在过去的30分钟里,我对我的舌尖有了一个想法,但我无法将其纳入代码中.请记住,这只是为了好玩!
来自Google的Python类:
E. Given two lists sorted in increasing order, create and return a merged
list of all the elements in sorted order. You may modify the passed in lists.
Ideally, the solution should work in "linear" time, making a single
pass of both lists.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案:
def linear_merge(list1, list2):
merged_list = []
i = 0
j = 0
while True:
if i == len(list1):
return merged_list + list2[j:]
if j == len(list2):
return merged_list + list1[i:]
if list1[i] <= list2[j]:
merged_list.append(list1[i]) …Run Code Online (Sandbox Code Playgroud) 我有几个排序列表,我想将它们一起添加到一个大的排序列表中.最有效的方法是什么?
这是我要做的,但效率太低:
big_list=[]
for slist in sorted_lists: # sorted_lists is a generator, so lists have to be added one by one
big_list.extend(slist)
big_list.sort()
Run Code Online (Sandbox Code Playgroud)
以下是sorted_lists的示例:
sorted_lists的大小= 200
sorted_lists的第一个元素的大小= 1668
sorted_lists=[
['000008.htm_181_0040_0009', '000008.htm_181_0040_0037', '000008.htm_201_0041_0031', '000008.htm_213_0029_0004', '000008.htm_263_0015_0011', '000018.htm_116_0071_0002', '000018.htm_147_0046_0002', '000018.htm_153_0038_0015', '000018.htm_160_0060_0001', '000018.htm_205_0016_0002', '000031.htm_4_0003_0001', '000032.htm_4_0003_0001', '000065.htm_5_0013_0005', '000065.htm_8_0008_0006', '000065.htm_14_0038_0036', '000065.htm_127_0016_0006', '000065.htm_168_0111_0056', '000072.htm_97_0016_0012', '000072.htm_175_0028_0020', '000072.htm_188_0035_0004'….],
['000018.htm_68_0039_0030', '000018.htm_173_0038_0029', '000018.htm_179_0042_0040', '000018.htm_180_0054_0021', '000018.htm_180_0054_0031', '000018.htm_182_0025_0023', '000018.htm_191_0041_0010', '000065.htm_5_0013_0007', '000072.htm_11_0008_0002', '000072.htm_14_0015_0002', '000072.htm_75_0040_0021', '000079.htm_11_0005_0000', '000079.htm_14_0006_0000', '000079.htm_16_0054_0006', '000079.htm_61_0018_0012', '000079.htm_154_0027_0011', '000086.htm_8_0003_0000', '000086.htm_9_0030_0005', '000086.htm_11_0038_0004', '000086.htm_34_0031_0024'….],
['000001.htm_13_0037_0004', '000008.htm_48_0025_0006', '000008.htm_68_0025_0008', '000008.htm_73_0024_0014', '000008.htm_122_0034_0026', '000008.htm_124_0016_0005', '000008.htm_144_0046_0030', '000059.htm_99_0022_0012', '000065.htm_69_0045_0017', '000065.htm_383_0026_0020', '000072.htm_164_0030_0002', …Run Code Online (Sandbox Code Playgroud)