I have two datasets which are in a dictionary format:
ch1 = {1000: 128,
2830: 1022,
3438: 198,
5908: 109}
ch2 = {1295: 1203,
2836: 1238,
4901: 8367,
7608: 249}
Currently, I have code to look for matches between keys in one dictionary and keys in the other:
coin = [int(ch1[key])+int(ch2[key]) for key in ch1.keys() & ch2.keys()]
I'm looking to change this code so that it finds keys that are within a given range of one another. For example, if within a range of 10, the output list from the example dictionaries would be [2260] as the sum of 1022+1238, by matching keys 2830 from dic1 and 2836 from dic2.
One limitation is the data files are large (~500Mb) which has limited the solutions I have thought iterating through list data.
In the rare case where there are two keys in one dictionary which are in the range of a key in the other dictionary, this should give one match.
ch1 = {1000: 128,
2830: 1022,
3438: 198,
5908: 109}
ch2 = {1295: 1203,
2836: 1238,
2839: 8367,
7608: 249}
Should still yield [5825], round((1238+8367)/2+1022).
In the even rarer case that there are two pairs, it does not matter which of these are matched together. There should be only two outputs in this case. Eg:
ch1 = {1000: 128,
2837: 1022,
2838: 198,
5908: 109}
ch2 = {1295: 1203,
2836: 1238,
2839: 8367,
7608: 249}
Result = [2260, 8565] which comes from 1022+1238, 198+8367