python - Using bisect for combing items with a distance condition -


in following 2 lists

 l1 = [10, 33, 50, 67]  l2 = [7, 16, 29, 65] 

i use bisect combine closest numbers in 2 lists. use code

for s in l1:     ind = bisect(l2, s, hi=len(l2) - 1)     ind -= abs(l2[ind-1] - s) < l2[ind] - s     print("{} -> {}".format(s, l2[ind])) 

this code gives output:

10 -> 7 33 -> 29 50 -> 65 67 -> 65 

in order rid of duplication of items in second list, used after print:

    if ind == len(l2) - 1: break   

but in case of if statement output is:

10 -> 7 33 -> 29 50 -> 65 

what want here combination done condition distance between closest numbers not greater 6, without breaking loop items fail test, in case of last code. want output:

10 -> 7 33 -> 29 50 -> -- # here condition 67 -> 65 

because have bisected know elements greater can test after subtracting:

from bisect import bisect s in l1:     ind = bisect(l2, s, hi=len(l2) - 1)     ind -= s - l2[ind-1] < l2[ind] - s     tmp = l2[ind]     print("{} -> {}".format(s,tmp if tmp  - s < 6 else "null")) 

using code original question:

with open("test.txt") f:     r = re.compile("(\d+)")     line in f:         a, b = line.lstrip("0123456789. ").split(">> ")         a_keys = [int(i.group()) in r.finditer(a)]         b_keys = [int(i.group()) in r.finditer(b)]         = a.strip("()\n").split(",")         b = b.strip("()\n").split(",")         ele, k in zip(a, a_keys):             ind = bisect(b_keys, k, hi=len(b) - 1)             ind -= k - b_keys[ind] < b_keys[ind-1] - k             print("{} -> {}".format(ele, b[ind] if abs(b_keys[ind] - k) < 5 else "null")) 

input:

1. (2- human rights, 10- workers rights)>> (3- droits de l'homme, 7- droit des travailleurs) 2. (2- human rights, 10- workers rights, 19- women rights)>> (1- droits de l'homme ,4- foobar, 15- les droits des femmes) 

output:

2- human rights -> 3- droits de l'homme  10- workers rights ->  7- droit des travailleurs 2- human rights -> 1- droits de l'homme   10- workers rights -> null  19- women rights ->  15- les droits des femmes 

if b_keys[ind] closest value + n less k have match, else output null.


Comments