I am looking for a country by ip range for tens of millions of lines. I am looking for a faster way to search.
I have 180K tuples in this form:
>>> data = ((0, 16777215, 'ZZ'), ... (1000013824, 1000079359, 'CN'), ... (1000079360, 1000210431, 'JP'), ... (1000210432, 1000341503, 'JP'), ... (1000341504, 1000603647, 'IN'))
(Integers are IP addresses converted to primes.)
This works correctly, but takes too much time:
>>> ip_to_lookup = 999 >>> country_result = [country ... for (from, to, country) in data ... if (ip_to_lookup >= from) and ... (ip_to_lookup <= to)][0] >>> print country_result ZZ
Can someone point me in the right direction for a faster way to do this search? Using the method above, 100 searches take 3 seconds. Sense, I think 10M lines will take several days.
python sorting search tuples
exzackley
source share