3.1 dictionary generation, set generation, generator

Keywords: Python

Dictionary generation:

  • Like list generation, dictionary generation is used to generate dictionaries quickly. The difference is that dictionaries need two values
#d = {key: value for (key, value) in iterable}
d1 = {'x': 1, 'y': 2, 'z': 3}
d2 = {k: v for (k, v) in d1.items()}
print(d2)

Set generation:

  • Set generative format is similar to list generative format, but with braces:
s1={x for x in range(10)}
print(s1)

 

Generator:

  • Generator is an iterative process to generate the corresponding elements of the iterative object
  • The element of the generator will not be generated before access, only when it is accessed. If you continue to access backward, the current element will be destroyed.
  • One way to generate a generator is to change the list generation to a bracket Wrap:
print("-----Use()List generation generator------")
g=(x*x for x in range(10))
print(type(g),g)
print(next(g),next(g),next(g))
  • Generator is essentially a function
  • When a generator is called, it returns a generator object without executing the function. When the next() method is called for the first time, the function executes downward and returns the value after yield if it is encountered. When the next() method is called again, the function continues to execute downward from the last ending position and returns the value after yield if it is encountered.
  • You can use yield to define a generator:
print("\n----Use yield generate generator-------")
def ge():
    print("For the first time yield")
    yield 1
    print("The second time yield")
    yield 2
    print("Third time yield")
    yield 3
o = ge()
print(next(o))
print(next(o))
print(next(o))

-----------------
//Operation result:
----Use yield generate generator-------
//First yield
1
//Second yield
2
//The third yield
3
  • Generator is essentially a function. If we want to get the return value of this function, we need to use exception capture to get the return value:
def fib(max):
    n,a,b = 0,0,1
    while n <max:
        yield b
        a,b =b,a+b
        n = n+1
    return 'done'

print("\n-----Try to get function return value------")
gg=fib(6)
while True:
    try:
        x=next(gg)
        print("g:",x)
    except StopIteration as e:
        print('Return value equal to:',e.value)
        break
  • You can use either next() to iterate over the generator or for:
def ge():
    print("For the first time yield")
    yield 1
    print("The second time yield")
    yield 2
    print("Third time yield")
    yield 3
o = ge()

print("\n---iteration generator Method--------")
for x in o:
    print(x)#Equivalent to entering generator Function, execute and get the return value


----
//Result:
---iteration generator Method--------
//First yield
1
//Second yield
2
//The third yield
3
  • Due to the characteristics of the generator, you can carry out "cooperation" operation: subsequent supplement

Posted by twm on Thu, 16 Apr 2020 07:48:46 -0700