Python Forum
Python - Search element in list without the value index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - Search element in list without the value index
#1
I am trying a binary search algorithm to search for an element in a list, but the low and high intervals should not be the index in list but rather the value of the item in the list. I have written a code for that and I've used the enumeration function but that is not efficient. Any alternatives I can implement this?

myList = [1,4,5,6,9,10,12,14,16,19,20,22,23,24,29,30]#list(range(1, 51))  #Create list between 0-100
search = int(input("Search For Number: "))  #Enter number to search for
low = int(input("Lower Interval: "))    #starting point in list
high = int(input("Higher Interval: "))  #end point in list

#look for closest index to low in the list
for index, item in enumerate(myList):
        if(item >= low):
            low = index
            break

#look for closest index to high in the list
for index, item in enumerate(myList):
        if(item >= high):
            high = index
            break

def binary(myList, low, high, search):
    
    print(myList[low:high], "\n")
    
    mid = int((low+high)/2) #mid point of two intervals within list

    if(low>high):   # Base condition: if number not in list, the lower will keep increasing
        print("Number not found!")
        return False            
    else:    
            if (search == myList[mid]): #base condition if number is the mid value
                print("number found in index number",mid)
                return mid
            elif (search < myList[mid]):    #return list from lower interval to mid point
                return binary(myList, low, mid-1, search)
            else:   #return list from mid point to higher interval
                return binary(myList, mid+1, high, search)


binary(myList, low, high, search)
Reply
#2
(Nov-04-2016, 12:31 AM)salmanfazal01 Wrote: I've used the enumeration function but that is not efficient.
Obviously if you use a O(n) method to attempt to speed up a O(logn) algorithm it isn't going to help. Basically your only choice to find those indexes is also a binary search. You can use the bisect module.

from bisect import bisect

myList = [1,4,5,6,9,10,12,14,16,19,20,22,23,24,29,30]

low = bisect(myList, int(input("Enter number with which to bisect: ")))

print(low)
Honestly I don't think this will help one bit, but it at least keeps the overall complexity of your code down to O(logn). O(logn) is incredibly fast so I don't think this will make any noticeable change whatsoever; just search the whole list. Also using an implementation of binary search to implement binary search is a little silly to say the least.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 322 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Search Excel File with a list of values huzzug 4 1,217 Nov-03-2023, 05:35 PM
Last Post: huzzug
  list in dicitonary element problem jacksfrustration 3 696 Oct-14-2023, 03:37 PM
Last Post: deanhystad
Thumbs Down I hate "List index out of range" Melen 20 3,307 May-14-2023, 06:43 AM
Last Post: deanhystad
  search an element in pandas series learningPython 5 1,406 Apr-30-2023, 08:34 AM
Last Post: learningPython
  Find (each) element from a list in a file tester_V 3 1,205 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Replace for loop to search index position illmattic 5 1,274 Sep-03-2022, 04:04 PM
Last Post: illmattic
  IndexError: list index out of range dolac 4 1,902 Jul-25-2022, 03:42 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020