伯克利 CS61A 课堂笔记 08 —— Strings and Dictionaries

本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。

目录

01 Strings 字符串

Ⅰ Strings are An Abstraction.

Ⅱ Strings Literals have Three Forms

Ⅲ String are Sequences

02 Dictionaries 字典

03 Dictionary Comprehensions 字典理解

附:词汇解释


01 Strings 字符串

Ⅰ Strings are An Abstraction.

>>> 'curry = lambda f: lambda x: lambda y: f(x, y)'
'curry = lambda f: lambda x: lambda y: f(x, y)'
>>> exec('curry = lambda f: lambda x: lambda y: f(x, y)')
>>> curry
<function <lambda> at 0x1003c1bf8>
>>> from operator import add
>>> curry(add)(3)(4)
7
Ⅱ Strings Literals have Three Forms
>>> 'I am string'
'I am string'
>>> '您好'
'您好'

>>> "I've learned a lot from CS61A"
"I've learned a lot from CS61A"

>>> """The Zen of Python 
claims, Readability counts. 
Read more: import this."""
'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'

① Single-quoted and double-quoted strings are equivalent.

② A backslash "escapes" the following character.

③ "Line feed" character represents a new line.

Ⅲ String are Sequences

Length and element selection are similar to all sequences.

However, the "in" and "not in" operators match substrings.

When working with strings, we usually care about whole words more than letters.

02 Dictionaries 字典

Dictionaries are collections of key-value pairs.

>>> numerals = {'I': 1, 'V': 5, 'X': 10}
>>> numerals
{'I': 1, 'V': 5, 'X': 10}

>>> numerals['I']
1
>>> numerals[0]
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
KeyError: 0
>>> numerals['V']
5
>>> numerals['X']
10

>>> list(numerals)
['I', 'V', 'X']
>>> numerals.values()
dict_values([1, 5, 10])
>>> list(numerals.values())
[1, 5, 10]
>>> sum(numerals.values())
16

>>> {}
{}
>>> {1: {}}
{1: {}}
>>> {1: 'item'}
{1: 'item'}
>>> {1: ['first', 'second'], 3: 'third'}
{1: ['first', 'second'], 3: 'third'}
>>> d = {1: ['first', 'second'], 3: 'third'}
>>> d[1]
['first', 'second']
>>> d[3]
'third'

>>> len(d)
2
>>> len(d[1])
2
>>> len(d[3])
5

Dictionary key do have two restrictions.

#Two keys cannot be equal
#There can be at most one value for a given key
>>> {1: 'first', 1: 'second'}
{1: 'second'}

#A key of a dictionary cannot be a list or a dictionary(or any mutable type)
#[] or {}
>>> {[1]: 'first'}
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
KeyError: unhashable type: 'list'

>>> {{}: 'first'}
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
KeyError: unhashable type: 'dict'

The first restriction is part of the dictionary abstraction.

The second restriction is tied to Python's underlying implementation of dictionaries.

If you want to associate multiple values with a key, store them all in a sequence value.

03 Dictionary Comprehensions 字典理解

An expression that evaluates to a dictionary using this evaluation procedure:

① Add a new frame with the current frame as its parent.

② Create an empty result dictionary that is the value of the expression.

③ For each element in the iterable value of <iter exp>:

A. Bind <name> to that element in the new frame from step 1.

B. If <filter exp> evaluates to a true value, then add to the result dictionary an entry that

pairs the value of <key exp> to the value of <value exp>.

Example 1: Indexing

def index(keys, values, match):
    """Return a dictionary from keys to a list of values for which 
    match(k, v) is a true value.
    
    >>> index([7, 9, 11], range(30, 50), lambda k, v: v % k == 0)
    {7: [35, 42, 49], 9: [36, 45], 11: [33, 44]}
    """
    return {k: [v for v in values if match(k, v) for k in keys]}

附:词汇解释

quote / kwoʊt / 引号

single-quote 单引号

double-quote 双引号

equivalent / ɪˈkwɪvələnt / 等同的

habitation 住所

backslash / ˈbækslæʃ / 反斜杠符号

escape / ɪˈskeɪp / 逃跑

mutable / ˈmjuːtəb(ə)l / 可变的

unhashable 不可哈希的

traceback 回溯

underlying 下层的,底层的

pair 配对

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/970088.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Stable Diffusion模型测试】测试ControlNet,没有线稿图?

相信很多小伙伴跟我一样&#xff0c;在测试Stable Diffusion的Lora模型时&#xff0c;ControlNet没有可输入的线稿图&#xff0c;大家的第一反应就是百度搜&#xff0c;但是能从互联网上搜到的高质量线稿图&#xff0c;要么收费&#xff0c;要么质量很差。 现在都什么年代了&a…

智能手表表带圆孔同心度检测

在智能手表的制造工艺中&#xff0c;表带圆孔同心度检测是确保产品品质的关键环节。精准的同心度不仅关乎表带与表体的完美适配&#xff0c;更直接影响用户的佩戴舒适度和产品的整体美观度。稍有偏差&#xff0c;就可能导致表带安装困难、佩戴时出现晃动&#xff0c;甚至影响智…

基于SSM+uniapp的数学辅导小程序+LW示例参考

1.项目介绍 系统角色&#xff1a;管理员、普通用户功能模块&#xff1a;用户管理、学习中心、知识分类管理、学习周报管理、口算练习管理、试题管理、考试管理、错题本等技术选型&#xff1a;SSM&#xff0c;Vue&#xff08;后端管理web&#xff09;&#xff0c;uniapp等测试环…

基于 openEuler 构建 LVS-DR 群集

一、 对比 LVS 负载均衡群集的 NAT 模式和 DR 模式&#xff0c;比较其各自的优势 。 二、 基于 openEuler 构建 LVS-DR 群集。 一 NAT 模式 部署简单&#xff1a;NAT 模式下&#xff0c;所有的服务器节点只需要连接到同一个局域网内&#xff0c;通过负载均衡器进行网络地址转…

JS设计模式之单例原型

那么单例模式都有哪些应用场景呢&#xff1f;如何通过构造函数创建单例如何使用模块化的方式创建总结 各位老铁们&#xff0c;今天我们介绍一下JS中单例设计模式&#xff0c;它的特点是确保一个类只有一个实例&#xff0c;并提供一个全局访问点来获取该实例&#xff08;无论被创…

vue+springboot+webtrc+websocket实现双人音视频通话会议

前言 最近一些时间我有研究&#xff0c;如何实现一个视频会议功能&#xff0c;但是找了好多资料都不太理想&#xff0c;最终参考了一个文章 WebRTC实现双端音视频聊天&#xff08;Vue3 SpringBoot&#xff09; 只不过&#xff0c;它的实现效果里面只会播放本地的mp4视频文件&…

Linux 基础IO——重定向和缓冲区

目录 一、重定向 1、重定向的本质 2、使用 dup2 系统调用 &#xff08;1&#xff09;输出重定向 &#xff08;2&#xff09;追加重定向 (3) 输入重定向 ​ 二、缓冲区 1.理解缓冲区 2.缓冲区刷新问题 3.为什么要有缓冲区&#xff1f; 4.这个缓冲区在哪里&#xff…

14、deepseek视觉大模型Janus Pro本地部署及实战

1、简介 2025.01.27&#xff1a; Janus-Pro发布&#xff0c;Janus的高级版本&#xff0c;显著提高了多模态理解和视觉生成。 Janus-Pro 是 Janus 的高级版本。具体来说&#xff0c; Janus-Pro 包括以下改进&#xff1a;优化的训练策略、 扩展的训练数据以及更大规模的模型。通…

【第3章:卷积神经网络(CNN)——3.1 CNN的基本结构与工作原理】

嘿,小伙伴们,今天咱们来聊聊深度学习里的一大明星——卷积神经网络(CNN)。这东西在图像识别、视频处理等领域简直不要太火,甚至人脸识别、物体检测这些高大上的应用,都离不开它的身影。废话不多说,咱们这就开聊! 一、CNN是什么东东? 在人工智能领域,卷积神经网络(…

VMware Workstate 的 Ubuntu18 安装 vmware tools(不安装没法共享)

在共享主机路径后&#xff0c;可以在&#xff1a; /mnt/hgfs/下方找到共享的文件。但没有安装vmware tool时是没法共享的。 如何安装vmware tool&#xff0c;网上版本很多。这里记录一下&#xff1a; VMware Workstation 17 Pro&#xff0c;版本&#xff1a;17.6.0 虚拟机系统…

高效开发!使用Chrome对MoonBit生成的Wasm进行性能分析!

在 [我们前一篇博客][call-wasm-from-js] 中&#xff0c;我们介绍了如何在前端 JavaScript 中使用 MoonBit 驱动的 Wasm 库 [Cmark]。在本文中&#xff0c;我们将探索如何直接从 Chrome 浏览器中对该库进行性能分析。希望这篇教程能对你在使用 MoonBit 在类似的场景中进行开发时…

《安富莱嵌入式周报》第350期:Google开源Pebble智能手表,开源模块化机器人平台,开源万用表,支持10GHz HRTIM的单片机,开源CNC控制器

周报汇总地址&#xff1a;嵌入式周报 - uCOS & uCGUI & emWin & embOS & TouchGFX & ThreadX - 硬汉嵌入式论坛 - Powered by Discuz! 视频版&#xff1a; https://www.bilibili.com/video/BV1YPKEeyEeM/ 《安富莱嵌入式周报》第350期&#xff1a;Google开…

2.【BUUCTF】bestphp‘s revenge

进入题目页面如下 进行代码审计 <?php // 1. 高亮显示当前PHP文件的源代码&#xff0c;方便开发者查看代码内容&#xff0c;在生产环境中不应使用此函数&#xff0c;可能会导致代码泄露。 highlight_file(__FILE__);// 2. 定义变量 $b &#xff0c;其值为字符串 implode &…

HarmonyOS:使用List实现分组列表(包含粘性标题)

一、支持分组列表 在列表中支持数据的分组展示&#xff0c;可以使列表显示结构清晰&#xff0c;查找方便&#xff0c;从而提高使用效率。分组列表在实际应用中十分常见&#xff0c;如下图所示联系人列表。 联系人分组列表 在List组件中使用ListItemGroup对项目进行分组&#…

【vue3】入门基础知识点

Vue3核心语法 组合式API【vue3】与选项式API【vue2】 setup setup和data、methods同级别, 可与data等共存&#xff0c;data里面可以读取使用setup中声明的变量&#xff0c;而setup不能使用data中声明的变量&#xff08;setup加载时间早于beforeCreated&#xff09;setup中的…

DeepSeek官方发布R1模型推荐设置

今年以来&#xff0c;DeepSeek便在AI领域独占鳌头&#xff0c;热度一骑绝尘。其官方App更是创造了惊人纪录&#xff0c;成为史上最快突破3000万日活的应用&#xff0c;这一成绩无疑彰显了它在大众中的超高人气与强大吸引力。一时间&#xff0c;各大AI及云服务厂商纷纷投身其中&…

M3U8工作原理以及key解密视频流详解

文章目录 前言一、M3U8是什么&#xff1f;二、HLS—M3U8的工作原理1.分段视频流2.生成播放列表3.客户端请求和解析4.片段下载和播放 三、.m3u8文件内部是什么样的&#xff1f;四、简单介绍下AES-128算法五、拿到KEY后如何去解密&#xff1f;1.手动解密.ts文件2.前人栽树&#x…

重读《Java面试题,10万字208道Java经典面试题总结(附答案)》

最近重读了这篇文章&#xff0c;对很多概念模糊的地方加了拓展和补充。 目录 1、JDK 和 JRE 有什么区别&#xff1f; 2、 和 equals 的区别是什么&#xff1f; 3、final 在 java 中有什么作用&#xff1f; 4、java 中的 Math.round(-1.5) 等于多少&#xff1f; 5、String…

AI知识库 - Cherry Studio

1 引言&#xff1a; 最近 DeepSeek 很火啊&#xff0c;想必大家都知道&#xff0c;DeepSeek 这个开源的模型出来后&#xff0c;因其高质量能力和R1 的思维链引发了大家本地部署的热潮。我也不例外&#xff0c;本地部署了一个 14B 的模型&#xff0c;然后把&#xff0c;感觉傻傻…

Ai笔记本-Ainote(IOS 应用)帮助支持页面

简介 一个 iCloud 实时同步的笔记工具&#xff0c;支持markdown 格式解析、分享 PDF文件。 方便存储各种AI生成的markdown 格式回答&#xff0c;自动保存到 iCloud 永不丢失&#xff0c;支持分享为 PDF 格式笔记。 联系方式 如果您在使用过程中有任何问题或建议&#xff0c;…