如何在python中对字符串元素的排序列表应用二进制搜索?

时间:2015-08-24 11:30:42

标签: python django binary-search

我有一个字符串元素(城市名称)的排序列表,我想在此实现二进制搜索,并通过给出首字母过滤掉城市?

例如用户输入:http://127.0.0.1:8000/api/?city=New

所以在这种情况下,我需要找到从New

开始的城市

示例输出:

[
"New Abbey|Ceredigion|United Kingdom",
"New Albany|Indiana|United States",
"New Albany|Kansas|United States",
"New Albany|Mississippi|United States",
"New Albany|Ohio|United States"
]

请告知。

3 个答案:

答案 0 :(得分:2)

以下方法应该有效。它使用Python自己的名为bisect的二进制搜索库来查找列表中的初始索引。对于搜索字词New,它会为我的示例列表返回2。然后,itertools.takewhile可用于返回条目,直到您的搜索字词失败为止:

import bisect, itertools

locations = [
    "Aaaa|aaaa|Test",
    "Bbbb|bbbb|Test",
    "New Abbey|Ceredigion|United Kingdom",
    "New Albany|Indiana|United States",
    "New Albany|Kansas|United States",
    "New Albany|Mississippi|United States",
    "New Albany|Ohio|United States",
    "Zzzz|zzzz|Test"
    ]

search = "New"
start_index = bisect.bisect_left(locations, search)
print list(itertools.takewhile(lambda x: x.startswith(search), itertools.islice(locations, start_index, None)))

提供以下输出:

['New Abbey|Ceredigion|United Kingdom', 'New Albany|Indiana|United States', 'New Albany|Kansas|United States', 'New Albany|Mississippi|United States', 'New Albany|Ohio|United States']

答案 1 :(得分:0)

您可以使用list comprehension过滤所需的项目:

[x for x in cities if x.startswith('New')]

答案 2 :(得分:0)

如果你想在python中实现二进制搜索,那么这可能对你有帮助。

def binarySearch(alist, item):
    first = 0
    last = len(alist)-1
    found = False

    while first<=last and not found:
         midpoint = (first + last)//2
         if alist[midpoint] == item:
             found = True
         else:
             if item < alist[midpoint]:
                 last = midpoint-1
             else:
                 first = midpoint+1

    return found

testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binarySearch(testlist, 3))    
print(binarySearch(testlist, 13))

来源:http://interactivepython.org/runestone/static/pythonds/SortSearch/TheBinarySearch.html