In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the advanced features of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Slice
List, tuple, string can be sliced
[start position: end position + 1: step size]
You can use a negative number, where-1 is the penultimate number.
When the step size is negative, it is separated from back to front, and the corresponding starting and ending position is reversed.
L = [1, 2, 3]
L = l [::-1]
# order reversal
# 3, 2, 1
Iteration
By default, dict iterates over key. If you want to iterate over value, you can use for value in d.values (), and if you want to iterate key and value at the same time, you can use for k, v in d.items ().
When we use a for loop, the for loop works as long as it acts on an iterable object, and we don't care much about whether the object is list or other data types.
You can use the Iterable type of the collections module to determine whether it is an iterable object
From collections import Iterable
Isinstance ("123", Iterable)
# True
What if you want to implement a Java-like subscript loop for list? Python's built-in enumerate function turns a list into an index-element pair, so you can iterate over both the index and the element itself in the for loop:
For I, value in enumerate (['A','B','C']):
Print (I, value)
# 0 A
# 1 B
# 2 C
In the for loop above, two variables are referenced at the same time, which is common in Python, such as the following code:
For x, y in [(1,1), (2,4), (3,9)]:
Print (x, y)
# 1 1
# 2 4
# 3 9
List generation
To generate list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you can use list (range (1,11)):
List (range (1,11))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[X * x for x in range (1,11)]
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# you can add if after the for loop to filter
[X * x for x in range (1,11) if x% 2 = = 0]
# [4, 16, 36, 64, 100]
# you can use a multi-layer loop to get a full arrangement
[M + n for m in 'ABC' for n in' XYZ']
# ['AX',' AY', 'AZ',' BX', 'BY',' BZ', 'CX',' CY', 'CZ']
The # for loop can be preceded by an if else expression
[x if x% 2 = 0 else-x for x in range (1,11)]
# [- 1,2,-3,4,-5,6,-7,8,9,10]
Generator generator
There are many ways to create a generator. The first method is very simple. Just change a list-generating [] to () and create a generator: how much is the http://mobile.sgyy029.com/ for Zhengzhou abortion surgery?
L = [x * x for x in range (10)]
L
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
G = x * x for x in range (10))
G
# at 0x1022ef630 >
# you can get the next return value of generator through the next () function
Next (g)
# 0
Next (g)
# 1
Next (g)
# 4
Next (g)
# 9
Next (g)
# 16
Generator saves the algorithm, and every time next (g) is called, it calculates the value of the next element of g until the last element is calculated, and when there are no more elements, the error of StopIteration is thrown.
After you create a generator, you basically never call next (), but iterate over it through the for loop, and you don't need to care about StopIteration errors.
Define generator:
# print the first N functions of Fibonacci
Def fib (max):
N, a, b = 0,0,1
While n < max:
Print (b)
A, b = b, a + b
N = n + 1
Return 'done'
# to change the fib function into generator, simply change print (b) to yield b
# this is another way to define generator. If a function definition contains the yield keyword, then the function is no longer an ordinary function, but a generator
Def fib (max):
N, a, b = 0,0,1
While n < max:
Yield b
A, b = b, a + b
N = n + 1
Return 'done'
The function is executed sequentially and returns when it encounters the declare statement or the last line of the function statement. Instead, the function that becomes generator executes each time next () is called, encounters a return of the yielding statement, and continues execution from the last returned yield statement when it is executed again.
Iterator
An object that can be called by the next () function and constantly returns the next value is called an iterator: Iterator.
It is called again and again by the next () function and returns the next value until finally a StopIteration error is thrown indicating that the next value cannot be returned.
Generators are all Iterator objects, but list, dict, and str are Iterable, but not Iterator.
To change an Iterable such as list, dict, str into Iterator, you can use the iter () function:
Isinstance (iter ([]), Iterator)
# True
Isinstance (iter ('abc'), Iterator)
# True
The calculation of Iterator is lazy, and it will only be evaluated when the next data needs to be returned.
Do not know the actual length of Iterator
This is the end of "what are the advanced features of Python"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.