Take a look at the three different "lists" below, guess the outputs:
(a)
>>> lists = [ [], [], [] ]
>>> lists
[[], [], []]
>>> lists[1].append("Hi there!")
>>> lists
?
(b)
>>> lists = [ [] for i in range(3) ]
>>> lists
[[], [], []]
>>> lists[1].append("Hi there!")
>>> lists
?
(c)
>>> lists = [ [] * 3 ]
>>> lists
[[], [], []]
>>> lists[1].append("Hi there!")
>>> lists
?
All same? Yeah, I do agree with you. However, here are what we actually get:
(a)
[[], ['Hi there!'], []]
(b)
[[], ['Hi there!'], []]
(c)
[['Hi there!'], ['Hi there!'], ['Hi there!']]
What has happened is that [[]]
is a one-element list containing
an empty list, so all three elements of [[]] * 3
are (pointers to)
this single empty list. Modifying any of the elements of lists
modifies this single list.
See more at
Chapter 3.6
- Sequence Types,
Python Library Reference
.
分享到:
相关推荐
1. **自动化任务**:Python的`os`、`shutil`和`subprocess`模块使得处理系统级别的任务变得简单,如文件操作、进程管理等。这些脚本可能包含了日常工作中重复性任务的自动化解决方案。 2. **数据分析**:Python的`...
- **amusing**:能够使人发笑或感到愉快的。 - **industrious/conscientious**:形容勤奋和尽责的工作者。 - **passionate**:对某事充满热情。 - **graceful**:优雅的动作或举止。 - **world-renowned**:...
- **amusing**:令人愉快的,引人发笑的,形容事物有趣或能让人开心。 - **amused**:感到好笑的,指个人对某事感到愉快或娱乐。 2. **句子结构与情景**: - 课件中提到的三个时间点(before, after, now)可能...
1,先做个基本的功能,展示出几个人们行业有趣的人2,搜索有趣的人 服务划分理念 采用为服务架构,将整个服务架构设定为宇宙模型,每个服务为一个星体,服务启动需要的配置是星体需要的物质组成服务的启动则为星体的...
After amusing myself by automating much of that job, I decided to return to school and study biol- ogy, which is when I took my first GIS course. I was instantly in love, and managed to convince ...
- **u[ju:]**:在"consumer", "accuse", "abuse", "amusing"中,字母u发[ju:]音,如句子"Consumers accuse them of abusing power, which is not amusing to them."。 - **u[u:]**:在"conclusion", "crucial", ...
1. **建议**:动词`advise`和名词`advice`之间的转换,如“他给了我一些建议”(He gave me some advice)。 2. **情感表达**:形容词`angry`变为副词`angrily`和名词`anger`,以及动词`annoy`和形容词`annoyed`,如...
1. **Academy** - 学院,常指专业教育机构,如艺术学院或军事学院;**academic** - 学术的,与学术研究或教学相关的;**actual** - 实际的,真实的;**actually** - 实际上,事实上。 2. **Appoint** - 指派,任命...
amusing.py
4. **情感色彩**:某些单词组合能引发情绪反应,例如 Group 6 的 “Consumers accuse them of abusing power, which is not amusing to them.” 这句话传达了一种不满的情绪,加深了对单词的记忆。 5. **对比与对照...
1. **generations** - 在这个题目中,"generations"指的是家族或社会中的一代人。这里指的是新的一代人(新一代)保持了古老的传统农业方法。 2. **departure** - "departure"在这里表示离开或背离传统,意味着不...
1. **thirsty** ['θɜːsti] - 表示“口渴的”,形容词,常用于表达生理需求或比喻渴望知识等抽象概念。 2. **honest** [ˈɒnɪst] - 意为“诚实的,正直的”,强调个人品质和道德标准。 3. **secret** ['si:krət...
* 介绍了英语句子的使用,例如:"Well, apart from talking, he can do some amusing actions, like dancing." 知识点四:英语演讲技巧 * 讲解了英语演讲技巧,例如:如何使用故事来讲解英语单词和句子结构 * 介绍...
1. amusing(adj.):好笑的,有趣的 示例:I had an amusing experience last year.(我去年有过一次有趣的经历。) 说明:amusing 可以用来形容事件、故事或经历,表示它们是有趣的或好笑的。 2. experience...
1. **词汇搭配与变形** - `amusing` (adj.) - `amused` (adj.) - `amuse` (v.) - `amusement` (n.) 这组词涉及“娱乐”相关的概念。`amusing` 形容事物使人觉得有趣,`amused` 形容人感到开心或娱乐,`amuse` 是...
文学领域中的关键词汇包括:literature(文学作品)、amusing(有趣的、逗乐的)、energetic(积极的、精力旺盛的)、intelligent(聪明的、理智的)等。 社会领域中的关键词汇包括:attitude(态度)、behaviour...
Additionally, there is a selection of pieces that are anecdotal, fanciful, or simply amusing. For this new edition, the authors have updated the original material while adding new material of ...
Additionally, there is a selection of pieces that are anecdotal, fanciful, or simply amusing. For this new edition, the authors have updated the original material while adding new material of ...
Additionally, there is a selection of pieces that are anecdotal, fanciful, or simply amusing. For this new edition, the authors have updated the original material while adding new material of ...