1

I have a sorted list of string elements(names of cities) and I would like to implement binary search on this and filter out cities by giving initial letters?

for example input by user: http://127.0.0.1:8000/api/?city=New

So here in this case I need to find out cities starting from New

Sample Output:

[
"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"
]

Please advise.

2
  • I think the link is supposed to be an example of input - looks like the OP's code extracts "New" from the url Commented Aug 24, 2015 at 11:37
  • import bisect --> look here: docs.python.org/3/library/bisect.html#module-bisect :) Commented Aug 24, 2015 at 12:01

3 Answers 3

2

The following approach should work. It uses Python's own binary search library called bisect to find the initial index into you list. For the search term New it returns 2 for my example list. itertools.takewhile can then be used to return entries until your search term fails:

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)))

Giving the following output:

['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']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Martin :)
0

you can use a list comprehension to filter the items you want:

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

5 Comments

That I've already done but my boss wants me to implement binary search.
that's incomprehensible. please don't dump code in comments.
you asked how to find cities starting with new - the answer does that. now you are asking about binary search - which is a very different thing - I think your boss may be confused
I already gave him two solutions, one using list comprehensions and one with the help of regular expression, but he says implement binary search on this which will decrease the search time. Currently it is an iterative search, if we implement binary search then time of searching cities shall reduce.
well... maybe. you'd want to have a really really bit list of cities for this to be a good use of your time.
0

If you want an implementation of binary search in python, then this might help you.

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))

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.