https://nychent.github.io/articles/2016-05/about-generator.cn

This is profound

Talking about Generator, Related to Concept Yes - {list, set, tuple, dict} comprehension and container - iterable - iterator - generator fuction and iterator - generator expression

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Spawn a Process: Chapter 3: Process Based Parallelism
import multiprocessing
import time
from collections import Iterable, Iterator
import dis
from itertools import islice
x = [1, 2, 3]
for i in x:
print i
y = iter(x)
z = iter(x)
print dir(x)
print dir(y)
print next(y)
print next(y)
print next(z)
print next(z)
print type(x)
print isinstance(x, Iterable)
print isinstance(x, Iterator)
print type(y)
print isinstance(y, Iterable)
print isinstance(y, Iterator)
class seq(object):
def __init__(self):
self.gap = 2
self.curr = 1
def __iter__(self):
return self
def next(self):
value = self.curr
self.curr += self.gap
return value
f = seq()
print list(islice(f, 0, 10))
def seq():
gap, curr = 2, 1
while True:
yield curr
curr += gap
f = seq()
print list(islice(f, 0, 10))
def fib():
a, b = 0, 1
while True:
yield b
a, b = b, a + b
print fib
f = fib()
print f
print(next(f), next(f), next(f), next(f), next(f))
def gen():
while True:
value = yield
print(value)
g = gen()
next(g)
g.send("hahahha")
next(g)

python Medium Iterable, Iterator, More articles on generator concepts

  1. python is、== difference ;with;gil;python in tuple and list The difference between ;Python Iterators in 、 generator 、 Decorator

    1. is The comparison is whether the two instance objects are exactly the same , Are they the same object , Whether the memory address occupied is the same == The comparison is whether the contents of two objects are equal 2. with Statement is used to try except finally The best of ...

  2. python Based on descriptor Some of the concepts of ( Next )

    @python Based on descriptor Some of the concepts of ( Next ) 3. Descriptor Introduce 3.1 Descriptor Code example 3.2 Definition 3.3 Descriptor Protocol( agreement ) 3 ...

  3. python Based on descriptor Some of the concepts of

    python Based on descriptor Some of the concepts of ( On ) 1. Preface 2. New and classic 2.1 Built in object object 2.2 Class method 2.2.1 Static methods 2.2.2 Class method 2.3 The new class (n ...

  4. python Based on descriptor Some of the concepts of ( On )

    @python Based on descriptor Some of the concepts of ( On ) python Based on descriptor Some of the concepts of ( On ) 1. Preface 2. New and classic 2.1 Built in object object 2.2 Class method 2 ...

  5. Python in sorted(iterable, /, *, key=None, reverse=False) What is the meaning of the diagonal bar in the parameter of ?

    adopt help(sorted) see sorted Help document for , It is shown as follows : Help on built-in function sorted in module builtins: sorted(iterab ...

  6. Finally understand Python Iterators and generators in !

    Iterators and generators Catalog Iterators and generators Iteratable objects and iterators Basic concepts Judge for The essence of the cycle Don't want to use for The loop iterates , How to use iterators ? List derivation generator Generator Concept How to implement and use ? generator ...

  7. Python Iterators in 、 generator

    from collections import Iterable, Iterator 1. Can the iteration (iterable) object Refer to the official website link class I: def __init__(self, v): ...

  8. python Iterators and generators in

    iterator The introduction of iterator If I had a list now l=['a','b','c','d','e'], I want to take the contents of the list , So there are several ways ? 1. Take value through index , Like l[0],l[1] 2. adopt for Value cycle fo ...

  9. python Iteration in 、 Generators and so on

    I know nothing about programming language ... Today, I read teacher Liao Xuefeng's about iteration , iterator , generator , Recursion, etc. ,word God , It's all about what ... 1. About iteration If given a list or tuple, We can go through for Cycle ...

Random recommendation

  1. COGS14. [ Network flow 24 topic ] With pilots

    [ Problem description ]      The flight brigade has a number of pilots from all over the world , Fly one type of plane , This kind of plane has two pilots per plane , We need a pilot and a co pilot . For a variety of reasons , For example, the problem of mutual cooperation , Some pilots can't fly on the same plane ...

  2. hdu 4768 Flyer Two points

    Ideas : Because at most one is odd , So the binary enumeration of this point , Every time we judge the sum of all the points in the left interval of this point as The basis of dichotomy . The code is as follows : #include<iostream> #include<cstd ...

  3. hdu1067-Gap(bfs+ Hash )

    Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit ...

  4. A good one png Compression tool pngquant Introduction Bulk compression png

    1.Png brief introduction . We know Png Yes 4 Channels ,RGBA, One more for transparency .png Yes png 32, 24, 8 etc. . What I understand roughly is how many colors . The smaller the number is. , The smaller the file is . A common way to compress is to import ...

  5. Dotfuscator Registration code and XenoCode Registration code

    .net Encryption tool registration code :Dotfuscator The registration code is as follows :     Your   serial   number   is   14705     Your   Confirmation   Numb ...

  6. linux yum install mongodb

    1.yum -y install mongodb-server  mongodb 2.service mongod start                     # start-up mongodb service 3. ...

  7. Apollo 6 — ConfigService Get the configuration interface

    The outline Before reading this article , Advice to see apollo Official documents of , Especially database design documents . Mainstream process analysis 2.1 Talk about the details 2.2 loadConfig() Load the configuration 2.3 auditReleases() Method record ...

  8. mybatis batch Bulk submission of large amounts of data

    Reprint :https://blog.csdn.net/Java_Mr_Zheng/article/details/50476757 stay xml File configuration multiple parameters inserted at the same time : <insert id=&quo ...

  9. Section 9 : Explain in detail Java The generics in , Multithreading , Network programming

    Preface Hello everyone , I'll give you a detailed explanation Java The generics in , Multithreading , An overview of network programming , Hope you like it Generic Generic format :ArrayList list= new ArrayList(); ArrayList list= ...

  10. JavaScript cookie Operation to realize the like function

    JavaScript cookie Operation to realize the like function Refer to the implementation principle , But the code is not concise enough , Simple code reference :js operation cookie It's very easy to implement a like function , The main problem is that you can't repeat the likes .  If it's a website with users , can ...