Finding fast default aliases in Python

Is there a faster way to do the following for much larger dicts?

aliases = {
            'United States': 'USA',
            'United Kingdom': 'UK',
            'Russia': 'RUS',
          }
if countryname in aliases: countryname = aliases[countryname]
+3
source share
4 answers

Your solution is perfect, as the word "in" is 0 (1) for dictionaries.

You can do something like this to save some typing:

countryname = aliases.get(countryname, countryname)

(But I find your code a lot easier to read than that)

When it comes to speed, which solution will best depend on whether there will be most hits or misses. But it will probably be in the nanosecond range when it comes to the difference.

+6
source

, dicts - . ., , :

countryname = aliases.get(countryname, countryname)

( , ) :

try:
    countryname = aliases[countryname]
except KeyError:
    pass
+2

.get ,

aliases.get(countryname)

countryname , None.

+1

, , , Bloom filter .

, (/ ), bisection root-finding.

First of all, I would definitely understand how Python implements dictionary searches, so you are not just reinventing the wheel.

In addition, their implementation in pure Python can be quite slow if it involves many iterations. Consider Cython, Numpy, or F2Py to get true optimization.

(if you are dealing only with country names, then I don’t think that you are dealing with comparisons large enough to justify any of my suggestions), but if you are looking at doing some kind of spell check check then ...

Good luck.

0
source

Source: https://habr.com/ru/post/1729771/


All Articles