0

I read this question about how to use bisect on a list of tuples and only compare the first value of tuple. It works, but how can I compare two value? if index of x < index of y and y[0] <= x[0] or y[1] <= x[1], bisect.bisect_left return index like

input:[(2, 2), (3, 1), (5, 6)]
bisect.bisect_left(input, (2, 3)) => 0
bisect.bisect_left(input, (3, 4)) => 1
bisect.bisect_left(input, (5, 5)) => 2
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
jacobcan118
  • 7,797
  • 12
  • 50
  • 95

1 Answers1

1

Bisect requires a sorted list. It will process tuples properly if the sorted order matches the comparison result between tuple entries.

This corresponds to x[0]<y[0] or x[0]==y[0] and x[1]<y[1].

Since you cannot sort your tuples on both values simultaneously, it will be impossible to obtain a y[0] <= x[0] or y[1] <= x[1] bisection result. Even if some combination of data allowed the sort, there would always be the possibility that a tuple (a,b) has two indexes in the list which constitutes an ambiguous result.

Alain T.
  • 40,517
  • 4
  • 31
  • 51