current position:Home>This paper clarifies the chaotic switching operation and elegant derivation of Python
This paper clarifies the chaotic switching operation and elegant derivation of Python
2021-08-23 04:10:31 【Coriander Chat Game】
Catalog
Because it's not used much at work python, So for python I don't know enough about , Only learn when you use it , In the previous examples, little was used python The special grammar of , Based on the principle of giving priority to completing tasks, there is no in-depth , But when reading other people's code, I find that some special syntax is not very familiar , I can't figure out what the code really means , Let's figure out the use of slicing and derivation today ,OK. Let's start
Remember : In the positive direction, the first one is 0, In the negative direction, the first one is -1, You can use the list as a replacement , Positive is 1 , Negative yes -1,0 It's the middle point
1、 Chaotic slicing operation
A complete slice expression contains two “:”, Used to separate three parameters (start_index、end_index、step), When there is only one “:” when , The third parameter is the default step=1.
Basic expression of slice operation :object[start_index : end_index : step]
step: Either positive or negative , Its absolute value determines the “ step ”, and The sign determines “ Cut the direction ”, It means “ From left to right ” Value , Negative representation “ From right to left ” Value . When step When omitted , The default is 1, That is, from left to right in increments 1 Value .“ Cutting direction is very important !”“ Cutting direction is very important !”“ Cutting direction is very important !”, Important things are to be repeated for 3 times !
start_index: Indicates the starting index ( Include the index itself ); When this parameter is omitted , To represent a subordinate “ Endpoint ” Start taking value , As for from “ The starting point ” Or from the “ End ” Start , By step The positive and negative determination of parameters ,step From “ The starting point ” Start , For the negative from “ End ” Start .
end_index: Indicates that the index is terminated ( Does not contain the index itself ); When this parameter is omitted , It means to fetch data all the way to ” Endpoint “, As for the arrival of ” The starting point “ Or to ” End “, By the same step The positive and negative determination of parameters ,step Set the timing until ” End “, It's negative until ” The starting point “.
In a word , final step Determines the direction , from startIndex To end_index Intercept .
for instance :
l = [1,2,3,4,5,6,7,8,9]
# 1、 Single index
print(l[2])
# 2、 Two indexes , positive
l2 = l[1:3]
print(l2)
# 3、 Two indexes , reverse
l3 = l[1:3:-1]
print(l3)
l4 = l[-1:-3:-1]
print(l4)
2、 Elegant derivation
# The syntax format of the list derivation is as follows :
# [ expression for Iterative variable in Iteratable object [if Conditional expression ] ]
# ==> for Iterative variable in Iteratable object
# expression
One sentence summary : What type of result is indicated by the outer parentheses , hinder for The previous statement runs during traversal
for instance :
# The series of values obtained by calculating the expression form a list .
list = [i for i in range(10) if i > 3]
print(list)
dict_b = {key: key * key for key in range(6)}
print(dict_b)
set_a = {value for value in ' There are people who are indifferent , Someone's carrying a load '}
print(set_a)
3、 summary
Slicing and derivation are python Common grammar in English , If it can be used flexibly, it can be used to force , You can also make the code simpler
copyright notice
author[Coriander Chat Game],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2021/08/20210823041028554G.html
The sidebar is recommended
- [Python introduction project] use Python to generate QR code
- Quickly build Django blog based on function calculation
- Python collects and monitors system data -- psutil
- Python interface test unittest usage details
- Implementation of top-level design pattern in Python
- You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
- Python simulation random coin toss (non optimized version)
- Using linear systems in python with scipy.linalg
- Using linear systems in python with scipy.linalg
- Together with Python to do a license plate automatic recognition system, fun and practical!
guess what you like
-
Using linear systems in python with scipy.linalg
-
Fast power modulus Python implementation of large numbers
-
Quickly build Django blog based on function calculation
-
You can easily get started with Excel pandas (I): filtering function
-
You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
-
You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
-
How does Python correctly call jar package encryption to get the encrypted value?
-
Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
-
Python simple Snake game (single player mode)
-
Using linear systems in python with scipy.linalg
Random recommended
- Python executes functions and even code through strings! Come and understand the operation of such a top!
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- [Python introduction project] use Python to generate QR code
- Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
- [windows] Python installation pyteseract
- [introduction to Python project] create bar chart animation in Python
- Python series tutorials 116
- Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
- Python Basics: do you know how to use lists?
- Solution of no Python 3.9 installation was detected when uninstalling Python
- [common links of Python & Python]
- [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
- [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
- [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
- Python socket implements UDP server and client
- Python socket implements TCP server and client
- leetcode 1974. Minimum Time to Type Word Using Special Typewriter(python)
- The mobile phone uses Python to operate picture files
- [learning notes] Python exception handling try except...
- Two methods of using pandas to read poorly structured excel. You're welcome to take them away
- Python sum (): the summation method of Python
- Practical experience sharing: use pyo3 to build your Python module
- Using Python to realize multitasking process