我有一个关于Linear Searching
in的问题Python
。假设我有基本代码
for l in lines:
for f in search_data:
if my_search_function(l[1],[f[0],f[2]]):
print "Found it!"
break
我们要确定search_data
存储在其中的值在哪里l[1]
。说my_search_function()
看起来像这样:
def my_search_function(search_key, search_values):
for s in search_values:
if search_key in s:
return True
return False
有什么办法可以提高处理速度?Binary
在这种情况下,搜索不起作用,因为行和search_data
是多维列表,我需要保留索引。我尝试了一种由外而内的方法,即
for line in lines:
negative_index = -1
positive_index = 0
middle_element = len(search_data) /2 if len(search_data) %2 == 0 else (len(search_data)-1) /2
found = False
while positive_index < middle_element:
# print str(positive_index)+","+str(negative_index)
if my_search_function(line[1], [search_data[positive_index][0],search_data[negative_index][0]]):
print "Found it!"
break
positive_index = positive_index +1
negative_index = negative_index -1
但是,我没有看到任何速度增加。有没有人有更好的方法?我希望将处理速度减半,因为我正在处理大量文件,CSV
并且一个文件的处理时间> 00:15,这是不可接受的,因为我正在处理 30 多个文件的批次。基本上我正在搜索的数据本质上是 SKU。from 的值lines[0]
可能类似于AS123JK
,并且该值的有效匹配可能是AS123
. 所以 HashMap 在这里不起作用,除非存在一种在 HashMap 查找中进行部分匹配的方法,这种方法不需要我分解诸如 之类的值['AS123', 'AS123J', 'AS123JK']
,这在这种情况下并不理想。谢谢!