Acquaintance Function - generator

  1. Generator is essentially an iterator, so the values ​​and way of iterators as iterator is just python comes, the generator is to write
  2. The pattern generator generates: a-- through the generator function; B - derivation; c - python module or built-in functions (a, c essence, are by generator function, a built-in 3, 1 is write your own)
  3. return和yield:
    • All return values, you can return more than just a one-off return multiple return
    • Perform termination function will return, after the function execution code does not return in vivo
    • yield records the current execution location: a yield corresponding to a next
  4. From the difference between yield and yield (Example 3.3 Example 3.4):
    • yield: a one-time return to the iterables
    • yield from: iterator object will return a

Example 3.1

def func():
print(1)
yield 2

() # function call func generator is generating nothing output .. If only the print (func ()) is a memory address generator
print (func () .__ next __ ()) # Since the generator is an iterative nature device, so the value is the same and iterators

Example 3.2

def func():
print(1)
yield 11
print(2)
yield 22

func()
print(func())
print(func().__next__())
print(func().__next__())

Example 3.3

def func():
lst = [1,2]
lst1 = ['alex','wusir','taibai','baoyuan']
yield lst
yield from lst1

func()
print(func().__next__()) # [1,2]
print(func().__next__()) # 'alex'
print(func().__next__()) # 'wusir'

Example 3.4

def func():
lst = [1,2]
lst1 = ['alex','wusir','taibai','baoyuan']
yield lst
yield from lst1

g = func()
for i in g:
print(i)

[1,2]

'alex'

'wusir'

'Taibai'

'Baoyuan'

Seeking [{x: y}] where x is an even number of tuples in the form of between 0-5,

y is an odd number between 0-5 membered group consisting of dictionary form

print({tuple([x for x in range(6) if x % 2 ==0]):
tuple([y for y in range(6) if y % 2 == 1])})

Guess you like

Origin www.cnblogs.com/lingshuai/p/11938935.html