


Several methods and performance comparison of Python to obtain the corresponding positions of letters in the alphabet
Several methods and performance comparison of python to obtain the corresponding positions of letters in the alphabet
In some cases, we are required to find out the order of letters in the alphabet, A = 1, B = 2, C = 3, and so on. , for example, this question https://projecteuler.net/problem=42 One of the steps to solve the problem is to convert the letters into the corresponding order in the alphabet.
The easiest way to get the corresponding position of letters in the alphabet is:
Use str.index or str.find method:
In [137]: "ABC".index('B') Out[137]: 1In [138]: "ABC".index('B')+1Out[138]: 2#或者在前面填充一个字符,这样index就直接得到字母序号: In [139]: "_ABC".index("B") Out[139]: 2
I also thought of converting the alphabet into a list or tuple and then indexing, performance Or will it improve? Or is it a good idea to store the key values composed of letters and numbers into a dictionary?
I also realized a method two days ago:
In [140]: ord('B')-64 Out[140]: 2
ord and chr are both built-in functions in python. ord can convert ASCII characters into serial numbers corresponding to the ASCII table, and chr can convert serial numbers into string.
The uppercase letters start from 65 in the table. Subtracting 64 is exactly the position of the uppercase letters in the table. Lowercase letters start at 97, and anything less than 96 is the corresponding alphabet position.
Which approach might be better in terms of 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和str.index的是一样的。这里就没必要写了。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 very practical functions, such as the %timeit function that is mainly used for text. Please enter pip install ipython to install.
The following is the result data of my test:
In [147]: %timeit azindexstr(text) 1 loop, best of 3: 9.09 s per loop In [148]: %timeit _azindexstr(text) 1 loop, best of 3: 8.1 s per loop In [149]: %timeit azindexlist(text) 1 loop, best of 3: 17.1 s per loop In [150]: %timeit azindexdict(text) 1 loop, best of 3: 4.54 s per loop In [151]: %timeit azindexdict2(text) 1 loop, best of 3: 1.99 s per loop In [152]: %timeit azord(text) 1 loop, best of 3: 2.94 s per loop In [153]: %timeit azand64(text) 1 loop, best of 3: 4.56 s per loop
From the results, it can be seen that list.index is the slowest, which surprised me. In addition, if there is a lot of data in the list, the index will be very slow. The speed of dict[r] is faster than that of dict.get(r), but if it is a non-existent key, dict[r] will report an error, while the dict.get method will not report an error and has better fault tolerance.
ord(r)-64’s method is fast and should be the most convenient to use, as there is no need to construct data.

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
