Thursday 15 January 2015

python - Binary Search Not finding value in List -


Not sure what is going on with my code to return a status to a list I have a normal binary search I have a list with the content

  ['drag_part_num', 'sku', 'name', 'creator' ',' Value ',' color 'diameter,' finish ',' made in USA ',' content ',' model ',' condition ',' specific application ',' style ',' type ',' width ',' Attachment ',' thumbnail ' 

and this There is a binary search:

  '' '' '' Def binary_search (self, a, x, lo = 0, hi = none): to specify the default for # hi It can not be used hi = hi if hi nobody else nobody else (a) # hi len (a) pos = bisect_left (A, X, lo, hi) # entry status return (status if pos! = Hi And a [pos] == x else -1) # Do not run off  

When I run the print self.binary_search (headerList, 'color') -1 lets not see how this is possible.

Thoughts?

For binary search to work, the list must be sorted:

  bisect import from bisect_left def binary_search (a, x, lo = 0, hi = none): can not be used to specify the default for # hi hi = en if hi none nobody else (A) # hi default lane (a) position = bisect_left (A, X, lo, h) # entry status return (position if pos = hi and one [pos] == x other -1) # 'sorted' '', '' '' '' '', '', '', '', '', '', '' Color, '' 'Diamonds', 'Finnish', 'Made in USA', 'Content', 'Model', 'Status', 'Exclusive App', 'Style', 'Type', 'Width', 'Act', 'Thumbnail' Print (header_list) print (binary_chatch (header_list, 'color'))  

Output:

  ['att_fitment', 'color', 'diameter', 'drag-part_nem ',' Finnish ',' Image '', 'Model', 'Name', 'Status', 'Price', 'simple_skus', 'skin', 'small IIM', 'specific application', 'style', 'thumbnail', 'type', 'type', 'width'] 1  

No comments:

Post a Comment