我有一个包含嵌套列表的列表,我需要知道在这些嵌套列表中进行搜索的最有效方法。
例如,如果我有
[['a','b','c'],
['d','e','f']]
我必须搜索上面的整个列表,找到'd‘的最有效方法是什么?
发布于 2012-08-15 11:23:31
使用生成器表达式,在生成器逐个生成结果时,不会遍历整个列表:
>>> lis = [['a','b','c'],['d','e','f']]
>>> 'd' in (y for x in lis for y in x)
True
>>> gen = (y for x in lis for y in x)
>>> 'd' in gen
True
>>> list(gen)
['e', 'f']
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in (y for x in lis for y in x)"
100000 loops, best of 3: 2.96 usec per loop
~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f'],[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]" "'d' in [y for x in lis for y in x]"
100000 loops, best of 3: 7.4 usec per loop
发布于 2012-08-15 11:37:53
如果您的数组始终如您所示那样排序,则a[i][j] <= a[i][j+1]
和a[i][-1] <= a[i+1][0]
(一个数组的最后一个元素始终小于或等于下一个数组中的第一个元素),则可以通过执行以下操作来消除大量比较:
a = # your big array
previous = None
for subarray in a:
# In this case, since the subarrays are sorted, we know it's not in
# the current subarray, and must be in the previous one
if a[0] > theValue:
break
# Otherwise, we keep track of the last array we looked at
else:
previous = subarray
return (theValue in previous) if previous else False
只有当你有很多数组,而且它们都有很多元素时,这种优化才是值得的。
https://stackoverflow.com/questions/11963711
复制相似问题