# This paper clarifies the chaotic switching operation and elegant derivation of Python

2021-08-23 04:10:31

Catalog

1、 Chaotic slicing operation

2、 Elegant derivation

3、 summary

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、 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