2

Is it a way in Python 3 to binary search in sorted list of lists?
Let's assume I have a list of lists:

list = [['A', 'B', 3], ['C', 'D', 1], ['E', 'F', 2]]

I've sorted it by 3d element in inner list with:

list = sorted(list , key=itemgetter(2))

and now list is

[['C', 'D', 1], ['E', 'F', 2], ['A', 'B', 3]]

And now how can I search in this sorted list with binary search (O(log(n)) time complexity) using element from inner list by which sorting was done? Like

findBy(list, index_of_inner_list, value_of_inner_list_to_find)

Outer list is huge. Inner lists have len 50. And I need to make many queries to extract some elements from outer list based on condition related to inner lists. I was thinking about bisect but inner arrays would be a problem for it I think.

aleksei
  • 315
  • 4
  • 14
  • 1
    Have you read this: https://stackoverflow.com/questions/42146482/using-bisect-on-list-of-tuples-but-compare-using-first-value-only where there are several suggestions for bisect implementations with an arbitrary key function. – user2390182 Oct 16 '17 at 12:55
  • Look at the last example on the [bisect](https://docs.python.org/3.6/library/bisect.html#other-examples) doc page. Does that look like what you're going for? – glibdud Oct 16 '17 at 12:55
  • @glibdud That example constructs a new list from the keys which destroys the `O(log_N)` performance of bisect which the OP explicitly asks for. – user2390182 Oct 16 '17 at 12:59
  • @glibdud Yes, I've read that and I was wondering is it a better way in Python for things like I need. Something like Comparator in Java. – aleksei Oct 16 '17 at 13:14
  • @aleksei I've the exact same question, what did you end up doing? – Abhijit Sarkar Aug 01 '21 at 06:23

0 Answers0