Python iterator generator and built-in function, anonymous function

  Today, I learned about iterator generators and built-in functions and anonymous functions. To be honest, it is a bit confusing and difficult.

  

1. Iterators and generators
1. How to get values ​​from lists and dictionaries:
index index
for loops
Anything that can use for loops to get values ​​is iterable
(1) Iterable protocol: internal __iter__ method All are iterable
(2) Iterator protocol: those with __iter__ methods and __next__ methods are iterators.
What is iterable: those with __iter__ methods are iterable.
What is an iterator : Iterator=iter (iterable), with a __next__ method
The biggest advantage of iterable: save memory
The range in py2 will generate a list regardless of the range, and this list will be used to store all values
​​in py3 Range does not actually generate any value regardless of the range.
Determine whether it is an iterable or an iterator:
from collections import Iterable,Iterator
print(range(10000))
print(isinstance(range(10000),Iterable)) whether Is iterable
print(isinstance(range(10000), Iterator)) is an iterator
? Advantages of iterators:
saving memory
and going to a value can be used for subsequent calculations, instead of waiting for all values ​​to be calculated before starting to connect The next operation - fast speed
Iterator characteristics: lazy operation
2, generator (Generator)
An iterator written by yourself is a generator
Two mechanisms for writing generators (iterators) by yourself: generator function, generator expression
(1) Generator function:
def cloth_g(num):
  for i in range(num) :
  yield 'cloth%s'%i

g = cloth_g(100)
print(next(g))
print(next(g))
Any function with yield is a generator function
. Yiled records the current position and waits for the next next to trigger the state
generator function of the function The call will not trigger the execution of the code, but will return a generator (iterator).
If you want the generator function to execute, you need to use next.
Example using a generator to listen to file input
import time
def listen_file():
  with open('file') as f:
    while True:
      line = f.readline()
      if line.strip():
        yield line.strip()
      time. sleep(0.1)
g = listen_file()
for line in g:
  print(line)
send keyword: pass a parameter to the generator function in the process of executing next.
I want to pass a value to the generator, there is an activation process, the first Once you must use next to trigger this generator
How to get a value from the generator
(1) next can stop at any time, and an error will be reported at the last time
(2) the for loop traverses from the beginning to the end, and will not stop if break and return are not encountered
(3) The forced conversion of the list tuple data type will load all the data into memory, wasting memory
. The generator function is a means for our python programmers to implement iteration. The
main feature is that the function contains yield
to call a generator function The code in this function will not be executed, only a generator (iterator) will be obtained.
Only when the value is taken from the generator, the code inside the function will be executed, and each time a piece of data is obtained, the data code will be executed to
obtain the data. The method includes next and send loops, and the data type is forced to convert the
return value of yield. If the loop itself is an iterable, and every element in the iterable data is to be returned, you can use yield from
to use send When the generator needs to be pre-excited after it is created, this step can be done using a decorator
Generator features: saving memory, lazy operation
Generators are used to solve memory problems and decoupling between program functions
3. Generator expressions, When there is a huge amount of data, use the generator expression
list = (out_exp_res for out_exp in input_list if out_exp == 2)
list comprehension, and use
list = [out_exp_res for out_exp in input_list if out_exp == 2]
out_exp_res: list when sorting Generate element expression, which can be a function with return value
for out_exp in input_list: iterate input_list and pass out_exp into out_exp_res expression
if out_exp == 2: filter which values ​​can be returned according to conditions
(1) A generator can only take a value once
(2) The generator never executes when it does not find the value it wants
(3) When it executes, it is subject to the values ​​of all variables at the time of execution


Second, built-in functions and anonymous functions
1. Built-in functions:
callable(o), o is a parameter, see if this variable is callable. o is the function name return True
dir() to see all names owned by a variable lock
bin(): binary, oct() octal, hex() hexadecimal
abs(): calculate absolute value
divmod(): quotient and remainder (quotient function)
enumerate(): display the index of the element
eval(): extract and execute
pow(): pow(2,3,3) #(2**3)%3 take the remainder of the cube of 2
reversed() : output the list in reverse order
. sort(): sort in ascending order
round(): default rounding, decimal precision, rounding
__import__: import module
id(o) through a string o is a parameter, returns the memory address of a variable
hash(o) o is a parameter that returns the hash value of a hashable variable. An error will be reported after a non-hashable variable is hashed.
sum(): Summation operation min
(): Calculate the minimum value max
(): Calculate the maximum value The position number finds its corresponding character AZ 65+26 ascii: it is the return value in the ascii code, otherwise it returns /u format('print element', '<20'): <left alignment,> right alignment, ^ center align




repr(): Returns the string form of an object (the original form is revealed)
enumerate: Enumeration receives two parameters: a container type, a serial number starting value
all(): In iterable objects, all Ture is Ture
any() : Among the iterable objects, there is a Ture that is Ture
for i in zip(): The function is used to take the iterable object as a parameter, pack the corresponding elements in the object into tuples, and then return the tuples composed of these tuples. List, the length of the list is the same as the shortest
print(self, *args, sep=' ', end='\n',file=None,flush=T/F)
file: default output to the screen, if set to file handle, Output to file
sep: print the separator between multiple values, the default is a space
end: the end of each print, the default is a newline
flush: immediately output the content to the stream file, no cache
hash Hash
(1) pair can be After hashing the data type of hash, a number will be obtained
(2) During the execution of a program, the result of hashing the same hashable variable will always be the same
(3) During the execution of a program, the same hashable variable will be hashed. The result of hash variable hashing is almost always different
(4) The underlying storage of the hash dictionary and the deduplication mechanism of the set collection are related
filter(): filter element
map:def func(num):
    return num ** 2
  for i in map(func,range(10)):print(i)
sorted(l,key=...):sort
2. Anonymous function
lambda: anonymous function keyword: function name = lambda parameter: return value
calc<function name> = lambda<define anonymous function keyword> n<parameter>:n**n<return value>
print(calc( 10))

3. Recursive call:
1. In the process of calling a function, recursive call directly or indirectly calls the function itself, which is called recursive call. There
are two necessary stages of recursion: 1. recursion 2, backtracking
2, recursive call must There must be a clear end condition
3. Every time you enter a deeper recursion, the scale of the problem should be reduced compared to the last recursion.
4. The recursion efficiency is not high.
Modify the maximum recursion depth
import sys
print(sys.setrecursionlimit(1000) )

4. Dichotomy
l = [18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
def func(l, num):
  mid = len(l) //2 #Intercept in the middle
  if num > l[mid]: #Determine whether the passed number is greater than mid
    func(l[(mid)+1:],num) #Greater than from the middle to Then find
  elif num < l[mid]: #Determine whether the passed number is less than mid
    func(l[0:(mid)],num) #It is greater than from the middle to find
  else:print('find it')
func(l ,66)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023139&siteId=291194637