Python - 2.2 Containers & 2.3 Formatting & 2.4 Sequences
来源:
https://dabeaz-course.github.io/practical-python/Notes/02_Working_with_data/02_Containers.html
https://dabeaz-course.github.io/practical-python/Notes/02_Working_with_data/03_Formatting.html
https://dabeaz-course.github.io/practical-python/Notes/02_Working_with_data/04_Sequences.html
scratch
/skrætʃ/
“Scratch” 是一个多义词,可以作为名词和动词使用,涵盖多种含义,包括物理上的划伤、启动某个动作的概念等。以下是“scratch”的主要用法和例句:
1. 动词用法: Scratch 作为动词,主要表示刮、抓或划的动作。
a) 刮、抓
表示用指甲、工具等轻轻地刮擦某物,通常用于身体表面或物体表面。
例句: He scratched his arm on the sharp edge of the table.
(他在桌子的锋利边缘划伤了手臂。)例句: The cat scratched the furniture.
(猫抓了家具。)b) 擦掉、涂去
表示用力擦拭以去除某种东西,例如文字或图案。
- 例句: She scratched her name off the list.
(她把自己的名字从名单上划掉了。)c) 开始或停止
在某些上下文中,表示从零开始或放弃某个计划。
- 例句: They had to scratch the project due to budget cuts.
(他们由于预算削减不得不放弃这个项目。)2. 名词用法: Scratch 作为名词,通常表示刮痕或抓伤。
a) 划痕、抓伤
表示由刮擦、抓取等动作造成的皮肤或物体表面的伤痕。
例句: There was a deep scratch on the car door.
(车门上有一道深划痕。)例句: He had a scratch on his cheek from the branches.
(他的脸颊上被树枝划伤了。)b) 起点
有时用于表示某个开始或起点的概念,尤其是在体育或游戏中。
- 例句: The runners started from scratch.
(跑步者从起点开始。)3. 其他用法:
Scratch card: 刮刮卡,一种游戏或彩票。
- 例句: I bought a scratch card hoping to win some money.
(我买了一张刮刮卡,希望能赢点钱。)Scratch that: 一种口语表达,意思是“算了”或“忘掉它”。
- 例句: Scratch that idea; we need a better plan.
(算了这个主意,我们需要一个更好的计划。)常见搭配:
- Scratch the surface: 表示只是触及某事的表面,未深入。
- Scratch one’s head: 表示思考或困惑。
- Scratch paper: 便条纸,用于草稿或随手记下东西的纸张。
总结:
- Scratch 作为动词表示刮、抓、涂去等动作,作为名词表示划痕、抓伤或起点。
- 该词的用法广泛,涵盖物理动作、计划放弃、起始点等多种情境。
Scratch 作为动词表示刮、抓、涂去等动作,作为名词表示划痕、抓伤或起点。
该词的用法广泛,涵盖物理动作、计划放弃、起始点等多种情境。
Building a list from scratch.
从头开始建立一个列表。
Neither … nor …
“Neither … nor …” 是一个常用的连接结构,用于表示两个否定的选项或事物。这种结构通常在句中用来强调两个事物都不成立或不具备某种特征。以下是它的用法和例句:
1. 基本用法
“Neither … nor …” 连接两个否定的词、短语或从句,表示两个选项都不成立。
结构:
- Neither + 名词/动词 + nor + 名词/动词
例句:
Neither the manager nor the employees were satisfied with the decision.
(经理和员工都对这个决定不满意。)Neither cats nor dogs are allowed in the restaurant.
(餐厅内不允许带猫和狗。)2. 与动词的一致性
在使用“neither … nor …”时,动词的形式通常与离它最近的主语一致。
例句:
Neither the teacher nor the students were prepared for the exam.
(老师和学生都没有为考试做好准备。)Neither the books nor the notebook is on the table.
(书和笔记本都不在桌子上。)3. 与副词的一致性
当“neither … nor …”结构中有副词时,副词通常放在两个部分之间。
例句:
- Neither the movie nor the book was even interesting.
(电影和书都没有一点意思。)4. 用于加强否定
“Neither … nor …” 可以用来强调对两个事物的否定。
例句:
Neither option seems appealing to me.
(这两个选择对我来说都没有吸引力。)I can neither confirm nor deny the rumors.
(我既无法确认也无法否认这些谣言。)总结
“Neither … nor …” 是一个表达否定的连接结构,用于表示两个选项或事物都不成立或不具备某种特征。在使用时,注意动词和主语的一致性。
Neither a list, a set, nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.
列表、集合或其他字典都不能充当字典键,因为列表和字典是可变的。
reinvent
reinvent == re + invent
invent = 发明,创造
re + invent = 在一次发明,再一次创造 == 彻底改造,重新创造。
there’s no need to reinvent the wheel here.
这里没有必要重新发明轮子。
yield
“yield” 是一个多义词,在不同的上下文中有不同的含义和用法。它可以作为动词或名词使用。以下是常见的用法:
1. 动词:生产、产生(产出某物)
这是 “yield” 最常见的用法之一,表示产生、生产或提供某种结果,通常是指农业、经济或科学实验中的产出。
例句:
- The farm yields hundreds of tons of wheat each year.
(这座农场每年生产数百吨小麦。)- The experiment yielded promising results.
(实验产生了有希望的结果。)2. 动词:让步、屈服
“yield” 也可以表示屈服或让步,通常是由于压力、劝说或更强的力量。
例句:
- After a long debate, he finally yielded to their demands.
(经过长时间的争论后,他最终屈服于他们的要求。)- The government refused to yield to pressure from protesters.
(政府拒绝屈服于抗议者的压力。)3. 动词:让路(交通中)
在交通规则中,“yield” 表示给其他车辆或行人让路。
例句:
- You must yield to pedestrians at the crosswalk.
(你必须在人行横道上让行给行人。)- Cars should yield to oncoming traffic at the intersection.
(汽车在十字路口应让行给对面来车。)4. 名词:产量、收益
作为名词,“yield” 通常表示某物的产量或投资的收益。
例句:
- The crop yield this year was higher than expected.
(今年的农作物产量高于预期。)- The bond has a yield of 5% per year.
(这支债券的年收益率是5%。)5. 动词:产生收益
在金融或经济领域,“yield” 还可以表示投资或资产产生收益的能力。
例句:
- The investment yields a good return every year.
(这项投资每年都带来不错的回报。)6. 短语动词:常见用法
yield to:屈服于,服从
- He finally yielded to the temptation.
(他最终屈服于诱惑。)yield up:交出,放弃
- The suspect yielded up the stolen goods.
(嫌疑人交出了赃物。)总结:
“yield” 可以用作动词或名词,意思涵盖生产产出、让步屈服、让路、以及产量或收益。它的具体含义取决于语境,常见于农业、经济、金融和交通领域的表达。
The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument.
enumerate对象产生一对,其中包含一个计数(从start开始,默认为零)和一个由iterable参数产生的值。
digression
di + gression
/daɪˈɡreʃn/
n.离题;脱轨
gression 移位
/'greʃən/
This section is a slight digression
这一段有点题外话
interval
/ˈɪntərv(ə)l/
“Interval” 是一个名词,通常表示间隔、时间段或距离。它用于描述两个事件、时间点、空间点或对象之间的差距或间隔。根据具体的语境,它可以用于不同领域,如音乐、数学、时间等。以下是“interval”的主要用法和例子:
1. 时间间隔
指的是两个时间点之间的时间长度,常用于描述两个事件发生之间的时间段。
例句:
The buses run at 15-minute intervals.
(公交车每隔15分钟运行一次。)There was a short interval between the two meetings.
(两次会议之间有一个短暂的间隔。)You should take a break at regular intervals during long study sessions.
(在长时间学习期间,你应该定期休息。)2. 空间或距离的间隔
表示两个物体或地点之间的物理距离或空间间隔。
例句:
The trees were planted at equal intervals along the road.
(沿着道路等距离种植了树木。)The seats in the theater are spaced at comfortable intervals.
(剧院的座位之间有舒适的间隔。)3. 数学中的间隔
在数学中,interval 通常表示两个数值之间的范围,尤其是在数轴上。例如,闭区间 [a, b] 表示 a 和 b 之间的所有数,包括 a 和 b。
例句:
- The solution lies within the interval [2, 5].
(解在区间 [2, 5] 内。)4. 音乐中的音程
在音乐中,interval 指的是两个音符之间的音高差异,称为音程。
例句:
- The song begins with an ascending major third interval.
(这首歌以上行大三度音程开始。)5. 休息间隔(特别用于表演或比赛)
在演出、比赛等场合,interval 指的是中场休息或演出、比赛中的暂停。
例句:
There will be a 10-minute interval during the concert.
(音乐会期间将有10分钟的中场休息。)The game resumed after a brief interval.
(比赛在短暂的中场休息后恢复。)6. 其他常见搭配
At regular intervals: 定期;每隔固定时间或距离。
- 例句: We stopped at regular intervals to rest during the hike.
(在徒步过程中,我们每隔一段时间就停下来休息。)Time interval: 时间间隔。
- 例句: The time interval between the two events was only a few seconds.
(两个事件之间的时间间隔只有几秒钟。)总结:
- Interval 可以表示时间、空间、数学范围、音高差异或表演中的休息,强调两个点、事件或对象之间的间隔。
Slices do not include the end value. It is like a half-open interval from math.
切片不包括最终值。它就像数学中的半开区间。
reduce
“Reduce” 是一个动词,常用来表示减少、缩减、降低等意思。它的用法比较广泛,以下是几种常见的用法:
1. Reduce + 名词
表示减少某物或某事物的数量、规模、程度等。
例句: The company aims to reduce costs by 20%.
(公司旨在将成本减少 20%。)例句: We need to reduce pollution to protect the environment.
(我们需要减少污染来保护环境。)2. Reduce (something) to (something)
表示把某物减少到某个程度、状态或条件。
例句: The building was reduced to rubble after the earthquake.
(地震后,建筑物变成了废墟。)例句: She was reduced to tears after hearing the sad news.
(听到坏消息后,她忍不住流泪了。)3. Reduce by + 数字/百分比
表示减少的具体数量或比例。
例句: The speed limit has been reduced by 10 miles per hour.
(限速已减少了每小时 10 英里。)例句: The price was reduced by 50%.
(价格降低了 50%。)4. Reduce to doing something
表示某事导致某人或某物进入一种不好的状态,常带有被迫的意味。
- 例句: The failure reduced him to begging for help.
(失败迫使他不得不乞求帮助。)5. Reduce (food or liquid)
在烹饪中,reduce 还可以表示通过加热减少液体的量,从而使其更加浓缩。
- 例句: You should reduce the sauce over medium heat until it thickens.
(你应该用中火把酱汁熬至变浓。)总之,“reduce” 的用法通常与减少某物的数量、程度、规模等相关,可以用于物质、数字、程度或者状态的改变。
There are some common functions to reduce a sequence to a single value.
有一些常用函数可以将序列简化为单个值
reduction
“Reduction” 是名词,表示“减少、缩小、降低”等意思。它通常与“reduce”这个动词对应,用来描述减少的行为或结果。以下是几种常见的用法:
1. Reduction in + 名词
表示某物或某事物的减少,通常指数量、程度、规模等的减少。
例句: There has been a significant reduction in crime rates this year.
(今年犯罪率显著下降。)例句: The company reported a 10% reduction in costs.
(公司报告了成本减少了 10%。)2. Reduction of + 名词
也可以表示某物的减少,但强调减少的量或具体事物。
例句: We aim for a reduction of greenhouse gas emissions.
(我们的目标是减少温室气体排放。)例句: The doctor recommended a reduction of salt in my diet.
(医生建议我减少饮食中的盐分。)3. Price reduction
用来表示价格的降低。
- 例句: Many stores offer big price reductions during the holiday season.
(许多商店在假日期间提供大幅度的价格折扣。)4. Reduction to + 名词
表示减少到某种状态,通常用来描述缩减或简化的结果。
- 例句: The reduction of the workforce to half its original size was necessary due to budget cuts.
(由于预算削减,员工人数减少到原来的一半是必要的。)5. Reduction in force (RIF)
这是一个专门术语,指由于经济困难或其他原因,公司进行的裁员行动。
- 例句: The company is planning a reduction in force to manage its costs.
(公司计划通过裁员来控制成本。)总结来说,“reduction” 主要表示减少的行为、过程或结果。它可以用于描述数量、规模、程度或价格等的减少。
Sequence Reductions
序列约简
novice
/ˈnɑːvɪs/
n.初学者,新手;
Sometimes the for
statement, len()
, and range()
get used by novices in some kind of horrible code fragment that looks like it emerged from the depths of a rusty C program.
Don’t do that! Don’t do that! Don’t do that!