1, using RE. Debug to view the matching process of regular expressions
Regular expressions are a big feature of Python, but it can be painful to debug, and it's easy to get a bug. Fortunately, Python can print out a parse tree of regular expressions, showing the complete process of re.compile through Re.debug.
Once you understand the grammar, you can find your mistakes. Here we can see [/font] forgot to remove []
2. The enumerate function is used to iterate through the elements in the list and their subscripts
3. Be careful with the default arguments
Instead, you should replace "[]" with a tag value that means "no definition".
4. For C-series developers who prefer parentheses rather than indentation, you only need to use one of the following commands:
From __future__ import Braces
5. Tricks in slicing operation
A = [1,2,3,4,5] >>> a[::2] [1,3,5]
The special example is x[::-1], which can reverse the list
>>> a[::-1] [5,4,3,2,1]
6, the decorative device
The adorner implements calling other functions or methods in one function to increase functionality, modifying parameters or results, and then adding adorners to the function definition with an "@" symbol.
The following example shows the use of a Print_args adorner:
7. The trick of taking the parameter
You can use * or * * To remove a list or dictionary as a function parameter
8. Exception Else statement
It is better to use "else" than to add extra code in a "try" statement because it avoids accidentally getting an exception that is not protected by a try statement ... In addition to the Declaration.
9. Nested list derivation and generator expression
[(I,J) for I in range (3) for J in Range (i)]
((I,J) for I in range (4) for J in Range (i))
These statements can replace a large number of nested loop code blocks
10. Main sentence patterns
Import this
Let's read again the essence of the Zen of Python (the Zen of Python, by Tim Peters):
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.