Python methods and performance comparisons for obtaining letters at corresponding locations in the alphabet

Source: Internet
Author: User
Tags ord
Python methods and performance comparisons for obtaining letters at corresponding locations in the alphabet

In some cases, we are asked to find out the alphabet in the order of the alphabet, a = 1,b = 2, C = 3, and so on, such as the topic https://projecteuler.net/problem=42 one step to solve the problem is to convert the letters to the corresponding order in the alphabet.

The easiest way to get the alphabet to correspond to the alphabet is to:

Use the Str.index or Str.find method:

In [137]: "ABC". Index (' B ') out[137]: 1In [138]: "ABC". Index (' B ') +1out[138]: C or fill in front of a character, so that index directly gets the letter ordinal: in [139]: "_ ABC ". Index (" B ") out[139]: 2

I also want to turn the alphabet into a list or a tuple again index, performance or will improve? Or is it a good idea to put the alphabet: A numeric key into the dictionary?

Two days ago I also had an epiphany of a method:

in [+]: Ord (' B ') -64out[140]: 2

Ord and CHR are all built-in functions in Python, and Ord can convert ASCII characters to the corresponding sequence number in the ASCII table, and CHR can turn the sequence number into a string.

Capital letters in the table are starting from 65, minus 64 is exactly where the uppercase letters are in the table. Lowercase letters start at 97, minus 96 is the corresponding alphabet position.

Which method might be better in performance? I wrote the code to test it:

AZ = "abcdefghijklmnopqrstuvwxyz" _az = "_abcdefghijklmnopqrstuvwxyz" azlist = List (az) azdict = dict (Zip (Az,range (1,27)) Text = az*1000000 #这个是测试数据 #str.find and Str.index are the same. There's no need to write. 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 into Ipython, and then use the Magic function%timeit to test the performance of each method. Ipython is a python interactive interpreter that comes with a variety of useful features, such as text-to-%timeit functionality. Please enter the PIP install Ipython installation.

Here is the result data for my test:

In [147]:%timeit azindexstr (text) 1 loops, Best of 3:9.09 s per loopin [148]:%timeit _azindexstr (text) 1 loops, Best of 3: 8.1 s per loopin [149]:%timeit azindexlist (text) 1 loop, Best of 3:17.1 s per loopin [max]:%timeit azindexdict (text) 1 lo OP, Best of 3:4.54 s per loopin [151]:%timeit azindexdict2 (text) 1 loops, Best of 3:1.99 s per loopin []:%timeit Azor d (text) 1 loops, Best of 3:2.94 s per loopin [153]:%timeit azand64 (text) 1 loops, Best of 3:4.56 s per loop

I was surprised to see that the List.index speed was the slowest from the results. In addition, if there is a lot of data in the list, index will be very slow. DICT[R] Faster than Dict.get (R), but if it is a non-existent key Dict[r] will error, and Dict.get method will not error, fault tolerance is better.

The method of Ord (R)-64 is very fast, and should be the most convenient to use, without having to construct data.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.