📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • dictionary
  • 在 for 循环中用 iter
  • Build-in Iterator Functions
  • 生成器(generator)
  • yield from

Was this helpful?

  1. CS 61A

Iterators

  • 容器可以提供迭代器(按顺序访问其元素)

  • iter(iterable): 在可迭代值的元素上返回一个迭代器

  • next(iterator): 返回迭代器中的下一个元素

  • 这个功能可以由 带 nonlocal index 的高阶函数实现

dictionary

  • 字典的 key, value, item(输出的是tuple) 都是可迭代的

  • 字典的 size 在 iter 中不能改变

在 for 循环中用 iter

  • iter 中已经输出的值不会再出现

  • range 每次都能输出想要的值

>>> for i in range(3,6):
>>>     print(i)
3
4
5

>>> for i in range(3,6):
>>>     print(i)
3
4
5

# 如果
>>> ri = iter(range(3,6))
>>> for i in ri:
    print(i)
3
4
5

>>> for i in ri:
>>>     print(i)

>>>

Build-in Iterator Functions

  • 许多内建的 Python 序列操作都会返回迭代器,这些迭代器会 lazily 地计算结果

    • map(func, iterable):: Iterate over func(x) for x in iterable

    • filter(func, iterable):: Iterate over x in iterable if func(x)

    • zip(first_iter, second_iter): : Iterate over co-indexed (x, y) pairs

    • reversed(sequence): : Iterate over x in a sequence in reverse order

  • 要查看迭代器的内容,将生成的元素放入一个容器中:

    • list(iterable): Create a list containing all x in iterable

    • tuple(iterable): Create a tuple containing all x in iterable

    • sorted(iterable): Create a sorted list containing x in iterable

>>> bcd = ['b', 'c', 'd']
>>> [x.upper() for x in bcd]  # 这是直接生成的情况
['B', 'C', 'D']

>>> caps = map(lambda x: x.upper(), bcd)  # 这是返回的迭代器 
>>> next(caps)
'B'
>>> next(caps)
'C'

生成器(generator)

  • 特殊的 iterator

  • 从生成器函数而来

def plus_minus(x):
"""Yield x and -x."""
    yield x
    yield -x

>>> t = plus_minus(3)
>>> next(t)
3
>>> next(t)
-3
>>> list(plus_minus(5))
[5, -5]
>>> list(map(abs, plus_minus(7)))
[7, 7]
  • 生成函数是一个产生值(yields values)而不是返回值(returning them)的函数。

  • 一个普通函数只 return 一次,一个生成函数可以 yield 多次。

  • 生成器是通过调用生成器函数自动创建的迭代器

  • 当调用生成器函数时,它返回一个生成器,该生成器对其 yields 进行迭代。

  • 生成函数和 迭代器 类似,都是 lazily 地运算的。因此 yield 后在那个框架会暂停

yield from

  • 生成器有时候也要输出迭代器

  • yield from 语句产生一个 迭代器 或 所有可迭代的值

def a_then_b_for(a, b):
    """The elements of a followed by those of b.

    >>> list(a_then_b_for([3, 4], [5, 6]))
    [3, 4, 5, 6]
    """
    for x in a:
        yield x
    for x in b:
        yield x

def a_then_b(a, b):
    """The elements of a followed by those of b.

    >>> list(a_then_b([3, 4], [5, 6]))
    [3, 4, 5, 6]
    """
    yield from a
    yield from b

def countdown(k):
    """Count down to zero.

    >>> list(countdown(5))
    [5, 4, 3, 2, 1]
    """
    if k > 0:
        yield k
        yield from countdown(k-1)
PreviousNon-Local AssignmentNextObjects

Last updated 4 years ago

Was this helpful?