Summary of knowledge points easily overlooked by Python
This article mainly introduces the knowledge points that are easy to ignore in Python. Examples of common operation skills that are easy to ignore in Python are analyzed. For more information, see
Here we record small issues that are easily overlooked in Python
I. input (...) and raw_input (...)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# The Difference Between the help document input (...) and raw_input (...) is as follows: >>> Help (input) Help on built-in function input in module _ builtin __: Input (...) Input ([prompt])-> value Equivalent to eval (raw_input (prompt )). >>> Help (raw_input) Help on built-in function raw_input in module _ builtin __: Raw_input (...) Raw_input ([prompt])-> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z + Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, Is printed without a trailing newline before reading. # It can be seen that the input will return values based on the input eval result. That is, if the input is a pure number, the result is a pure number. # Raw_input returns a string. # Test: >>> A = input ("input number ") Input Number 1 >>> Type () <Type 'int'> >>> B = raw_input ("input number ") Input Number 1 >>> Type (B) <Type 'str'> |
Ps: In Versions later than python3.0, raw_input and input are combined to cancel raw_input and replace it with input. Therefore, input in the current version receives strings.
Ii. python three-object Operator
Although Python does not have the three-object operator (? :), But there are similar alternatives,
That is
1. true_part if condition else false_part
?
1 2 3 4 5 6 7 8 |
>>> 1 if True else 0 1 >>> 1 if False else 0 0 >>> "True" if True else "False" 'True' >>> "True" if True else "False" 'Falser' |
2. (condition and [true_part] or [false_part]) [0]
?
1 2 3 4 5 |
>>> (True and ["True"] or ["False"]) [0] 'True' >>> (False and ["True"] or ["False"]) [0] 'False' >>> |
3. Obtain the nth index of the specified string in the entire string
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#-*-Coding: cp936 -*- Def findStr (string, subStr, findCnt ): ListStr = a. split (subStr, findCnt) If len (listStr) <= findCnt: Return-1 Return len (string)-len (listStr [-1])-len (subStr) # Test A = "12345 (1) 254354 (1) 3534 (1) 14" Sub = "(1 )" N = 2 # Find the location where the first occurrence occurs Print findStr (a, sub, N) N = 10 # locate the first occurrence Print findStr (a, sub, N) # Result #>>> #14 #-1 |
Iv. enumerate usage:
When traversing a sequence, you may need to use the index and corresponding value of the sequence at the same time. In this case, you can use the enumerate method to traverse the sequence.
Enumerate is described as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
>>> Help (enumerate) Help on class enumerate in module _ builtin __: Class enumerate (object) | Enumerate (iterable [, start])-> iterator for index, value of iterable | | Return an enumerate object. iterable must be another object that supports | Iteration. The enumerate object yields pairs containing a count (from | Start, which defaults to zero) and a value yielded by the iterable argument. | Enumerate is useful for obtaining an indexed list: | (0, seq [0]), (1, seq [1]), (2, seq [2]),... | | Methods defined here: | | _ Getattribute __(...) | X. _ getattribute _ ('name') <=> x. name | | _ Iter __(...) | X. _ iter _ () <=> iter (x) | | Next (...) | X. next ()-> the next value, or raise StopIteration | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | _ New _ = <built-in method _ new _ of type object> | T. _ new _ (S,...)-> a new object with type S, a subtype of T |
V. Methods of traversing Sequences
?
1 2 3 4 5 6 7 |
>>> List = ['A', 'B', 'C'] >>> For index, value in enumerate (List ): Print index, value 0 1 B 2 c >>> |
6. Use the sample function of the python random module to randomly select a group of elements from the list.
?
1 2 3 4 5 6 |
Import List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Slice = random. sample (List, 5) # Obtain 5 elements randomly from the List and return them as a part. Print slice Print List # The original sequence has not changed. |
7. Print a list dictionary containing Chinese Characters in json format.
?
1 2 3 4 5 6 |
#-*-Coding: UTF-8 -*- Import json # Your list ListA = [{'path ': ['[AWS] \ xe7 \ xbe \ x8e \ xe5 \ xb0 \ x91 \ xe5 \ xa5 \ xb3 \ xe6 \ x88 \ x98 \ xe5 \ xa3 \ xab Sailor Moon Crystal-Moon pride MV [BIG5] [BDrip 1080 p x264 aac000000006e5cfe860000.mp4 '], 'length': 131248608L}, {'path ': ['[AWS] \ xe7 \ xbe \ x8e \ xe5 \ xb0 \ x91 \ xe5 \ xa5 \ xb3 \ xe6 \ x88 \ x98 \ xe5 \ xa3 \ xab Sailor Moon Crystal-Moon pride MV [BIG5] [BDrip 720 p x264 aac00000000639d304a0000.mp4 '], 'length': 103796306l}, {'path ': ['[AWS] \ xe7 \ xbe \ x8e \ xe5 \ xb0 \ x91 \ xe5 \ xa5 \ xb3 \ xe6 \ x88 \ x98 \ xe5 \ xa3 \ xab Sailor Moon Crystal-Moon pride MV [BIG5] [BDrip 480 p x264 aac1_rj5a81baca316.mp4 '], 'length': 75198408L}] # Print the list Print json. dumps (listA, encoding = 'utf-8', ensure_ascii = False) |
Output result:
?
1 2 |
>>> [{"Path": ["[AWS] girly girl warrior Sailor Moon Crystal-Moon Pride MV [BIG5] [BDrip 1080 p x264 aac1_1_6e5cfe862.16.mp4"], "length": 131248608 }, {"path": ["[AWS] Beautiful girl warrior Sailor Moon Crystal-Moon Pride MV [BIG5] [BDrip 720 p x264 aac000000000000639d304a0000.mp4"], "length": 103166306 }, {"path": ["[AWS] girly girl warrior Sailor Moon Crystal-Moon Pride MV [BIG5] [BDrip 480 p x264 aac0000000000005a81baca0000.mp4"], "length": 75198408}] |
I hope this article will help you with Python programming.