I have a short list (List A) and a long one (List B), and I need to find inside of List B the closest match to each of List A's numbers. Pic related.
Except I have one List A about 150 entries long, and about 100 List Bs about 10k entries long, so it would take me a very long time to do them all by hand like in the example.
>Binary sorting I can't program. Is there some software that I can use?
Evan Thomas
For each i in A: For each j in B: find the minimum (i - j) squared
You can use the excel functions to find this.
Jace Wilson
I don't understand this but I'll Google it, thank you.
Liam Foster
There are functions in excel.
There is a min function.
Matthew Sanchez
I'll make a script that does this for you for 5$ bucks.
Oliver Campbell
I'm finding some results, thank you very much.
I'd accept your offer if I could. Unfortunately I'm a neet and I own almost nothing, so I can't pay you.
Andrew Fisher
I would basically take the entire List A and then loop through List B checking if each number in B is closer than the previous for all in List A.
So you would assign 117 to all in the first item of List A, then in the second loop check if 124 is closer to the number in List A than 117 was, and continue. So you're looping List A inside List B.
The check can be a simple subtraction of B from A, changed to a positive number, if the result is smaller then the new number takes the place.
Parker Green
Am I retarded or is this all op needs: arr_a =sorted( [n1, n2, n3, ...]) arr_b = sorted([n1, n2, n3, ...])
for a, b in zip(arr_a, arr_b): print(a, 'closest to: ', b)
Ian Collins
But I can't code, so I'm asking for some software that does it for me.
I'm currently trying in Excel
Nicholas Wilson
Nope. It's that easy.
Nicholas Lee
Feel like this would match 261 to 124...
Elijah Miller
Can you match two items from list A to one item from list B?
It took me a while because my version is not in Italian so I had to translate the function and change a few things, but it seems to be working.
Thanks a lot to everyone who tried to help. I really appreciate it.
Josiah James
Glad you managed it, here's what I could come up with in python. #!/usr/bin/env python3
A = [118, 261, 315, 395] B = [117, 124, 257, 296, 348, 351, 395, 408]
S = dict()
for a, b in [ (a, b) for a in A for b in B ]: if a not in S or abs(a - b) < abs(a - S[a]): S[a] = b
print(S)
Oliver White
I can't find words for how atrocious this is. I also tried to make the same thing work, and my Excel is in Russian, and apparently it requires all function to be written using Russian functions names, English ones don't work. What was Microsoft thinking? I'm sure this was not a thing in earlier versions of Excel, when did this tsart?
Elijah Rogers
Thank you. Just out of curiosity, with this you need to paste in the tho lists for each script (since there are several List Bs), correct?
Yeah, it took me an hour to fiddle with words and punctuation to make it work. I guess it's for your average accountants and cubicle workers who don't know English and would be totally confused by weird words that don't make sense, but for people who do things in English it's an absolute pain. Especially if there are no resources in your language for what you need to do.
Ian Young
it's just a mockup, we could easily read the lists from text files, one number by line
Christian Sanchez
Ah, neat.
Charles Davis
>Just out of curiosity, with this you need to paste in the tho lists for each script (since there are several List Bs), correct? The best idea, I guess, would be to put List A into the script and then pass List B to the script at runtime, or have it read from a file that you just change out.
Dylan Harris
I see, thank you.
Jonathan Clark
Now it reads all files in the "data" folder, one number per line, and outputs the corresponding numbers from the first list that has to be hardcoded in the script
#!/usr/bin/env python3
import os path = 'data' A = [118, 261, 315, 395]
for f in os.listdir(path): B = [ int(line) for line in open(path + '/' + f) ] S = dict() for a, b in [ (a, b) for a in A for b in B ]: if a not in S or abs(a - b) < abs(a - S[a]): S[a] = b for v in S.values(): print(v) print('---')
Benjamin Powell
>for a, b in [ (a, b) for a in A for b in B ]: stop this shit import itertools
for x, y in itertools.product(a,b): print(x, y)
James Myers
it's that bad?
Sebastian Evans
how about this:
var arr_a = [118, 261, 315, 395] var arr_b = [117, 124, 257, 296, 348, 351, 395, 408]
Thank you, but I already solved it with Excel (see ). Also I can't code, so I don't really know what to do with that, but I appreciate your effort, thank you very much.