首页 > 技术文章 > [2021 Spring] CS61A 学习笔记 lecture 10 containers and sequences

ikventure 2021-06-19 21:50 原文

lecture 10 containers,
课本:http://composingprograms.com/pages/21-introduction.html
http://composingprograms.com/pages/23-sequences.html
前几分钟继续lecture 9没讲完的Exercise: Tracing和装饰器部分(更新到上一篇学习笔记lecture 9了)
这节课主要是数据类型的过渡,引入序列类型和相关特性。

Containers

由数字类型的数据过渡到数据容器:由其他数据组成的数据。

Simple Pairs

将一对数值表示成一个单独的数值:

  • Construct new pairs from two values.
  • Select one or the other of the two values used to construct the pair.

Fun Side Trip: Pairs of Integers

Another Representation

Adding Mutability

Attempt #1

注意a, b不是pair_func frame中的变量,不能在其中重新赋值,需要nonlocal。

Nonlocal

Attempt #2

Sequences 序列

以上基于函数的containers很复杂且效率低,python提供了更方便的表示方式-序列(以下内容python语言设计基础课程一般都有讲解,也可以查询菜鸟教程)。

  • 序列可以是有限的,也可以是无限的。
  • 序列可以是可变的,也可以是不可变的。
  • 序列可以是加索引的:元素可以通过索引获取
  • 序列可以是可迭代的:值可以从头到尾依次获取

Python’s Sequences

python的序列类型:元组、列表、字符串、range、迭代器和生成器。
(集合类型:集合和字典)

Construction 结构

String Literals 字符串

Selection and Slicing 索引和切片

Sequence Combination and Conversion 组合和转换

Sequence Iteration: For Loops for循环

推荐阅读