Python methods for obtaining letters in the corresponding location of the alphabet and Performance Comparison of several methods for obtaining letters in the corresponding location of the alphabet in python and performance comparison
In some cases, we need to find out the alphabetic order of letters, A = 1, B = 2, C = 3, and so on, for example, the https://projecteuler.net/problem=42 of this question, one step to solve the problem step is to convert the letter into the corresponding sequence of the alphabet.
The easiest way to obtain the location of a letter in the alphabet is:
Use str. index or str. find:
In [1, 137]: "ABC ". index ('B') Out [137]: 1In [138]: "ABC ". index ('B') + 1Out [138]: 2 # or enter a character In front of the index to directly obtain the letter number: In [139]: "_ ABC ". index ("B") Out [1, 139]: 2
I also want to convert the alphabet to list or tuple and then index, or will the performance be improved? Or is it a good way to store letters: numbers in a dictionary?
Two days ago, I realized a method:
In [140]: ord('B')-64Out[140]: 2
Both ord and chr are built-in functions in python. ord can convert ASCII characters into numbers corresponding to the ASCII table, while chr can convert the numbers into strings.
In uppercase letters, the table starts from 65 and the position of the uppercase letters in the table is exactly the 64-bit. Lowercase letters start from 97, and 96 is the corresponding alphabet.
Which method may have better performance? I wrote code to test it:
Az = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _ az = "_ ABCDEFGHIJKLMNOPQRSTUVWXYZ" azlist = list (az) azdict = dict (zip (az, range (1,27 ))) text = az * 1000000 # This is the test data # str. find and str. the index is the same. There is no need to write it here. Def azindexstr (text): for r in text: az. index (r) + 1 passdef _ azindexstr (text): for r in text: _ az. index (r) passdef azindexlist (text): for r in text: azlist. index (r) passdef azindexdict (text): for r in text: azdict. get (r) passdef azindexdict2 (text): for r in text: azdict [r] passdef azord (text): for r in text: ord (r) -64 passdef azand64 (text): for r in text: ord (r) % 64 pass
Copy and paste the above code to ipython, and then use the magic function % timeit to test the performance of each method. Ipython is a python interactive interpreter with a variety of useful functions, such as the main % timeit function of text. Enter pip install ipython for installation.
The following are the results of my test:
In [147]: %timeit azindexstr(text)1 loop, best of 3: 9.09 s per loopIn [148]: %timeit _azindexstr(text)1 loop, best of 3: 8.1 s per loopIn [149]: %timeit azindexlist(text)1 loop, best of 3: 17.1 s per loopIn [150]: %timeit azindexdict(text)1 loop, best of 3: 4.54 s per loopIn [151]: %timeit azindexdict2(text)1 loop, best of 3: 1.99 s per loopIn [152]: %timeit azord(text)1 loop, best of 3: 2.94 s per loopIn [153]: %timeit azand64(text)1 loop, best of 3: 4.56 s per loop
From the results, we can see that the speed of list. index is the slowest. I was surprised. In addition, if the list contains a large amount of data, the index will be very slow. Dict [r] is faster than dict. get (r) is fast, but if it is a non-existent key dict [r], an error is reported, while dict. the get method does not report errors and has better fault tolerance.
The ord (r)-64 method has a good speed. it is also the most convenient to use and does not need to construct data.