Article Outline

# 数据容器

## 迭代（Iterate）

``````for c in 'Python':
print(c)``````
``````P
y
t
h
o
n``````

``````# Written in C
char *string;

scanf("%s",string);
int i=strlen(string);
int k = 0;
while(k<i){
printf("%c", string[k]);
k++;
}``````

``````for i in range(10):
print(i)``````
``````0
1
2
3
4
5
6
7
8
9``````

``````var i;
for (i = 0; i < 10; i++) {
console.log(i)
}``````

``````[...Array(10).keys()].forEach((x) => {
console.log(x)
})``````

## 列表（List）

### 列表的生成

``````a_list = []
b_list = [1, 2, 3]
list() # or list(iterable) # 这是 Type Casting
[(expression with x) for x in iterable]``````
``````a_list = []
a_list.append(1)
a_list.append(2)
print(a_list, f'has a length of {len(a_list)}.')

#range() 返回的不是 list，需要用 list() 转换，否则也没办法调用 .append()
b_list = list(range(1, 9))
b_list.append(11)
print(b_list, f'has a length of {len(b_list)}.')

c_list = [2**x for x in range(8)]
print(c_list, f'has a length of {len(c_list)}.')``````
``````[1, 2] has a length of 2.
[1, 2, 3, 4, 5, 6, 7, 8, 11] has a length of 9.
[1, 2, 4, 8, 16, 32, 64, 128] has a length of 8.``````

``````a = [2**x for x in range(8)]
print(a)``````

Comprehend 这个词的意思除了 “理解” 之外，还有另外一个意思，就是 “包括、囊括” —— 这样的话，你就大概能理解这种做法为什么被称作 List Comprehension 了。中文翻译中，怎么翻译的都有，“列表生成器”、“列表生成式” 等等，都挺好。但是，被翻译成 “列表解析器”，就不太好了，给人的感觉是操作反了……

List comprehension 可以嵌套使用 `for`，甚至可以加上条件 `if`。官方文档里有个例子，是用来把两个元素并不完全相同的列表去同后拼成一个列表（下面稍作了改写）：

``````import random

n = 10

# 生成一个 n 个元素的序列，每个元素是 1~100 之间的随机数
a_list = [random.randrange(1, 100) for i in range(n)]
print(f'a_list comprehends {len(a_list)} random numbers: {a_list}')

# 从 a_list 里把偶数都挑出来
b_list = [x for x in a_list if x % 2 == 0]
print(f'... and it has {len(b_list)} even numbers: {b_list}')``````
``````a_list comprehends 10 random numbers: [52, 34, 7, 96, 33, 79, 95, 18, 37, 46]
... and it has 5 even numbers: [52, 34, 96, 18, 46]``````

### 列表的操作符

• 拼接：`+`（与字符串不一样的地方是，不能用空格 `' '` 了）
• 复制：`*`
• 逻辑运算：`in``not in``<``<=``>``>=``!=``==`

``````a_list = [1, 2, 3]
b_list = [4, 5, 6]
c_list = a_list + b_list * 3
c_list
7 not in c_list
a_list > b_list``````
``````[1, 2, 3, 4, 5, 6, 4, 5, 6, 4, 5, 6]
True
False``````

### 根据索引提取列表元素

``````import random
n = 3
a_list = [random.randrange(65, 91) for i in range(n)]
b_list = [chr(random.randrange(65, 91)) for i in range(n)]
print(a_list)
c_list = a_list + b_list + a_list * 2
print(c_list)

print()
# 根据索引提取（Slicing）
print(c_list[3])        # 返回索引值为 3 的元素值
print(c_list[:])        # 相当于 c_list，返回整个列表
print(c_list[5:])       # 从索引为 5 的值开始直到末尾
print(c_list[:3])       # 从索引 0 开始，直到索引 3 之前（不包括 3）
print(c_list[2:6])      # 从索引 2 开始，直到索引 6 之前（不包括 6）

print()
# 根据索引删除
del c_list[3]
print(c_list)           # del 是个命令，del c_list[3] 是一个语句；不能这么写：print(del c_list[3])
del c_list[5:8]
print(c_list)

print()
# 根据索引替换
c_list[1:5:2] = ['a', 2]  # s[start:stop:step] = t，跟 range 的三个参数类似；
# len(t) = len([start:stop:step]) 必须为真
print(c_list)``````
``````[77, 66, 79]
[77, 66, 79, 'L', 'Z', 'R', 77, 66, 79, 77, 66, 79]

L
[77, 66, 79, 'L', 'Z', 'R', 77, 66, 79, 77, 66, 79]
['R', 77, 66, 79, 77, 66, 79]
[77, 66, 79]
[79, 'L', 'Z', 'R']

[77, 66, 79, 'Z', 'R', 77, 66, 79, 77, 66, 79]
[77, 66, 79, 'Z', 'R', 77, 66, 79]

[77, 'a', 79, 2, 'R', 77, 66, 79]``````

``````s = 'Python'[2:5]
print(s)
del s[2]  # 这一句会报错``````
``````tho

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-7-c9c999709965> in <module>
1 s = 'Python'[2:5]
2 print(s)
----> 3 del s[3]  # 这一句会报错

TypeError: 'str' object doesn't support item deletion``````

``````s = 'Python'
L = list(s)
print(s)
print(L)
del L[2]
print(L) # 用 del 对 L 操作之后，L 本身少了 1 个元素``````
``````Python
['P', 'y', 't', 'h', 'o', 'n']
['P', 'y', 'h', 'o', 'n']``````

### 列表可用的内建函数

• `len()`
• `max()`
• `min()`
``````import random
n = 3

# 生成 3 个随机数，构成一个列表
a_list = [random.randrange(65, 91) for i in range(n)]
b_list = [chr(random.randrange(65, 91)) for i in range(n)]
print(a_list)
print(b_list)

# 列表可以使用操作符 + 和*
c_list = a_list + b_list + a_list * 2
print(c_list)

a_list *= 3
print(a_list)

# 内建函数操作 len()、max()、min()
print(len(c_list))
print(max(b_list)) # 内建函数内部做了异常处理，可以比较字符和数字 —— 初学者最讨厌这种事情了……
print(min(b_list)) # 注意，max() 和 min() 应用的是 b_list, len() 应用的是 c_list —— 请自行找到对应的 list 进行分析。

print('X' not in b_list)``````
``````[89, 84, 85]
['X', 'B', 'X']
[89, 84, 85, 'X', 'B', 'X', 89, 84, 85, 89, 84, 85]
[89, 84, 85, 89, 84, 85, 89, 84, 85]
12
X
B
False``````

### Methods

``````import random
n = 10
a_list = [random.randrange(1, 100) for i in range(n)]
print(f'a_list comprehends {len(a_list)} random numbers:\n', a_list)

a_list.sort()
print('the list sorted:\n', a_list)

a_list.sort(reverse=True) #reverse 参数，默认是 False
print('the list sorted reversely:\n', a_list)``````
``````a_list comprehends 10 random numbers:
[98, 9, 95, 15, 80, 70, 98, 82, 88, 46]
the list sorted:
[9, 15, 46, 70, 80, 82, 88, 95, 98, 98]
the list sorted reversely:
[98, 98, 95, 88, 82, 80, 70, 46, 15, 9]``````

``````import random
n = 10

a_list = [chr(random.randrange(65, 91)) for i in range(n)]
# chr() 函数会返回指定 ascii 码的字符，ord('A') 是 65
print(f'a_list comprehends {len(a_list)} random string elements:\n', a_list)

a_list.sort()
print('the list sorted:\n', a_list)

a_list.sort(reverse=True) #reverse 参数，默认是 False
print('the list sorted reversely:\n', a_list)

print()

b_list = [chr(random.randrange(65, 91)) +\
chr(random.randrange(97, 123))\
for i in range(n)]
# 可以在行末加上 \ 符号，表示 “该行未完待续……”

print(f'b_list comprehends {len(b_list)} random string elements:\n', b_list)

b_list.sort()
print('the sorted:\n', b_list)

b_list.sort(key=str.lower, reverse=True)
# key 参数，默认是 None
# key=str.lower 的意思是，在比较排序的时候，先全都转换成小写再比较排序……
# —— 但并不改变原有值
print('the sorted reversely:\n', b_list)``````
``````a_list comprehends 10 random string elements:
['B', 'U', 'H', 'D', 'C', 'V', 'V', 'Q', 'U', 'P']
the list sorted:
['B', 'C', 'D', 'H', 'P', 'Q', 'U', 'U', 'V', 'V']
the list sorted reversely:
['V', 'V', 'U', 'U', 'Q', 'P', 'H', 'D', 'C', 'B']

b_list comprehends 10 random string elements:
['Nl', 'Mh', 'Ta', 'By', 'Ul', 'Nc', 'Gu', 'Rp', 'Pv', 'Bu']
the sorted:
['Bu', 'By', 'Gu', 'Mh', 'Nc', 'Nl', 'Pv', 'Rp', 'Ta', 'Ul']
the sorted reversely:
['Ul', 'Ta', 'Rp', 'Pv', 'Nl', 'Nc', 'Mh', 'Gu', 'By', 'Bu']``````

NOTE

``````a_list = [1, 'a', 'c']
a_list = a_list.sort() # 这一句会报错``````
``````---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-12-acb9480a455d> in <module>
1 a_list = [1, 'a', 'c']
----> 2 a_list = a_list.sort() # 这一句会报错

TypeError: '<' not supported between instances of 'str' and 'int'``````

``````import random
n = 3
a_list = [random.randrange(65, 91) for i in range(n)]
b_list = [chr(random.randrange(65, 91)) for i in range(n)]
print(a_list)
c_list = a_list + b_list + a_list * 2
print(c_list)

# 在末尾追加一个元素
c_list.append('100')
print(c_list)

# 清空序列
print()
print(a_list)
a_list.clear()
print(a_list)

print()
# 拷贝一个列表
d_list = c_list.copy()
print(d_list)
del d_list[6:8]
print(d_list)
print(c_list)             # 对一个拷贝操作，不会更改 “原件”

print()
# 演示拷贝 .copy() 与赋值 = 的不同
e_list = d_list
del e_list[6:8]
print(e_list)
print(d_list)             # 对 e_list 操作，相当于对 d_list 操作

# 在末尾追加一个列表
print()
print(a_list)
a_list.extend(c_list)      # 相当于 a_list += c_list
print(a_list)

# 在某索引位置插入一个元素
print()
print(a_list)
a_list.insert(1, 'example')   # 在索引 1 的位置插入 'example'
a_list.insert(3, 'example')   # 在索引 3 的位置插入 'example'；
print(a_list)

# 排序

# a_list.sort() 这一句会出错，因为当前列表中的元素，是 int 和 str 混合的。

print()
print(a_list)
a_list.reverse()
print(a_list)
x = a_list.reverse() # reverse() 只对当前序列操作，并不返回一个逆序列表；返回值是 None
print(x)``````
``````[90, 88, 73]
[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73]
[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']

[90, 88, 73]
[]

[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']
[90, 88, 73, 'T', 'N', 'Y', 73, 90, 88, 73, '100']
[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']

[90, 88, 73, 'T', 'N', 'Y', 88, 73, '100']
[90, 88, 73, 'T', 'N', 'Y', 88, 73, '100']

[]
[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']

[90, 88, 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']
[90, 'example', 88, 'example', 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']

[90, 'example', 88, 'example', 73, 'T', 'N', 'Y', 90, 88, 73, 90, 88, 73, '100']
['100', 73, 88, 90, 73, 88, 90, 'Y', 'N', 'T', 73, 'example', 88, 'example', 90]
None``````

``````import random
n = 3
a_list = [random.randrange(65, 91) for i in range(n)]
print(a_list)

# 插入
print()
a_list.insert(1, 'example')   # 在索引 1 的位置插入 'example'

# 删除
print()
print(a_list)
a_list.remove('example')      # 去除 'example' 这个元素，如果有多个 'example'，只删除第一个
print(a_list)

# pop() 删除并返回被删除的值

print()
print(a_list)
p = a_list.pop(2)      # 去除索引为 2 的元素，且返回元素的值，赋值给 p
print(a_list)
print(p)

# pop() 与 del，或者 remove() 的区别
print()
a_list.insert(2, 'example')
a_list.insert(2, 'example')
print(a_list)
del a_list[2]
print(a_list)

print()
print(a_list.remove('example')) # a_list.remove() 这个 Method 的返回值是 None
print(a_list)``````
``````[88, 84, 69]

[88, 'example', 84, 69]
[88, 84, 69]

[88, 84, 69]
[88, 84]
69

[88, 84, 'example', 'example']
[88, 84, 'example']

None
[88, 84]``````

## 元组（Tuple）

• List 是可变有序容器，Tuple 是不可变有序容器。
• List 用方括号标识 `[]`，Tuple 用圆括号 标识 `()`

``a = ()``

``````a = 1, 2, 3   # 不建议这种写法
b = (1, 2, 3) # 在创建元组的时候建议永远不省略圆括号……
print(a)
print(b)
print(a == b)``````
``````(1, 2, 3)
(1, 2, 3)
True``````

NOTE

``````a = 2, # 注意这个末尾的逗号 , 它使得 a 变量被定义为一个元组，而不是数字
a

b = 2  # 整数，赋值
b

c = (2) # 不是元组
c
type(c) # 还是 int

d = (2,) # 这才是元组
d
a == d``````
``````(2,)
2
2
int
(2,)
True``````

``````a = 1,
print(a)
print(id(a))
a += 3, 5
print(a)
print(id(a)) # id 并不相同 —— 实际上是在内存中另外新创建了一个元组……``````
``````(1,)
4593032496
(1, 3, 5)
4592468976``````

``````n = 10000 #@param {type:"number"}
a = range(n)
b = tuple(a) # 把 a 转换成元组
c = list(a) # 把 a 转换成列表
a.__sizeof__() # GitPress 中的 Python 不支持 __sizeof__() 方法
b.__sizeof__()
c.__sizeof__()``````
``````48

80024

90088``````

## 集合（Set）

``````primes = {2, 3, 5, 7, 11, 13, 17}
print(primes)``````
``{2, 3, 5, 7, 11, 13, 17}``

### 创建

``````a = {} # 注意这样创建的是一个 dict（字典），而不是 set 集合
b = set() # 这样创建的才是空集合
print(type(a))
print(type(b))``````
``````dict
set``````

``````a = "abcabcdeabcdbcdef"
b = range(10)
c = [1, 2, 2, 3, 3, 1]
d = ('a', 'b', 'e', 'b', 'a')
print(set(a))
print(set(b))
print(set(c))
print(set(d))``````
``````{'a', 'b', 'c', 'd', 'e', 'f'}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3}
{'a', 'b', 'e'}``````

Set 当然也可以进行 Comprehension

``````a = "abcabcdeabcdbcdef"
b = {x for x in a if x not in 'abc'}
print(b)``````
``{'d', 'e', 'f'}``

### 操作

• 并集：`|`
• 交集：`&`
• 差集：`-`
• 对称差集：`^`

``````admins = {'Moose', 'Joker', 'Joker'}
moderators = {'Ann', 'Chris', 'Jane', 'Moose', 'Zero'}``````

``````admins = {'Moose', 'Joker', 'Joker'}
moderators = {'Ann', 'Chris', 'Jane', 'Moose', 'Zero'}

print('Joker' in moderators)  # Joker 是否是 moderator？
``````{'Joker', 'Moose'}
True
False
{'Ann', 'Chris', 'Jane', 'Joker', 'Moose', 'Zero'}
{'Moose'}
{'Joker'}
{'Ann', 'Chris', 'Jane', 'Joker', 'Zero'}``````
``````# 这个 cell 集合运算图示需要安装 matplotlib 和 matplotlib-venn，请在本地计算机运行
# !pip install matplotlib
# !pip install matplotlib-venn
import matplotlib.pyplot as plt
from matplotlib_venn import venn2

moderators = {'Ann', 'Chris', 'Jane', 'Moose', 'Zero'}

plt.show()``````

``````admins = {'Moose', 'Joker', 'Joker'}
moderators = {'Chris', 'Moose', 'Jane', 'Zero'}

``````{'Chris', 'Jane', 'Joker', 'Moose', 'Zero'}
{'Moose'}
{'Joker'}
{'Chris', 'Jane', 'Joker', 'Zero'}``````

### 逻辑运算

set `==` other

`True`: set 与 other 相同

set `!=` other

`True`: set 与 other 不同

isdisjoint(other)

`True`: set 与 other 非重合；即，`set & other == None`

issubset(other)，set `<=` other

`True`: set 是 other 的子集

set `<` other

`True`: set 是 other 的真子集，相当于 `set <= other && set != other`

issuperset(other)，set `>=` other

`True`: set 是 other 的超集

set `>` other

`True`: set 是 other 的真超集，相当于 `set >= other && set != other`

### 更新

remove(elem)

pop(elem)

clear() 从集合中删除所有元素。

set.update(*others)，相当于 `set |= other | ...`

set.intersection_update(*others)，相当于 `set &= other & ...`

set.difference_update(*others)，相当于 `set -= other | ...`

set.symmetric_difference_update(other)，相当于 `set ^= other`

### 冻结集合

https://docs.python.org/3/library/stdtypes.html#frozenset

## 字典（Dictionary）

Map 是容器中的单独一类，映射（Map）容器。映射容器只有一种，叫做字典（Dictionary）。先看一个例子：

``````phonebook = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225}
print(phonebook)``````

``````phonebook = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225}
print(phonebook['bob'])``````
``8982``

``````phonebook = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}
print(phonebook)``````
``{'ann': 6585, 'bob': 8982, 'joe': 2598, 'zoe': 1225}``

### 字典的生成

``````aDict = {}
bDict = {'a':1, 'b':2, 'c':3}
print(bDict)``````
``````{}
{'a': 1, 'b': 2, 'c': 3}``````

### 更新某个元素

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}

print(phonebook1['joe'])
phonebook1['joe'] = 5802
print(phonebook1)
print(phonebook1['joe'])``````
``````2598
{'ann': 6585, 'bob': 8982, 'joe': 5802, 'zoe': 1225}
5802``````

### 添加元素

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}
phonebook2 = {'john':9876, 'mike':5603, 'stan':6898, 'eric':7898}

phonebook1.update(phonebook2)
print(phonebook1)``````
``````{'ann': 6585,
'bob': 8982,
'joe': 2598,
'zoe': 1225,
'john': 9876,
'mike': 5603,
'stan': 6898,
'eric': 7898}``````

### 删除某个元素

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}

del phonebook1['ann']
print(phonebook1)``````
``{'bob': 8982, 'joe': 2598, 'zoe': 1225}``

### 逻辑操作符

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}

print('ann' in phonebook1)

print(phonebook1.keys())
print('stan' in phonebook1.keys())

print(phonebook1.values())
print(1225 in phonebook1.values())

print(phonebook1.items())
print(('stan', 6898) in phonebook1.items())``````
``````True
dict_keys(['ann', 'bob', 'joe', 'zoe'])
False
dict_values([6585, 8982, 2598, 1225])
True
dict_items([('ann', 6585), ('bob', 8982), ('joe', 2598), ('zoe', 1225)])
False``````

### 可用来操作的内建函数

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}
phonebook2 = {'john':9876, 'mike':5603, 'stan':6898, 'eric':7898}
phonebook1.update(phonebook2)

print(len(phonebook1))
print(max(phonebook1))
print(min(phonebook1))
print(list(phonebook1))
print(tuple(phonebook1))
print(set(phonebook1))
print(sorted(phonebook1))
print(sorted(phonebook1, reverse=True))``````
``````8
'zoe'
'ann'
['ann', 'bob', 'joe', 'zoe', 'john', 'mike', 'stan', 'eric']
('ann', 'bob', 'joe', 'zoe', 'john', 'mike', 'stan', 'eric')
{'ann', 'bob', 'eric', 'joe', 'john', 'mike', 'stan', 'zoe'}
['ann', 'bob', 'eric', 'joe', 'john', 'mike', 'stan', 'zoe']
['zoe', 'stan', 'mike', 'john', 'joe', 'eric', 'bob', 'ann']``````

### 常用 Methods

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}
phonebook2 = {'john':9876, 'mike':5603, 'stan':6898, 'eric':7898}

phonebook3 = phonebook2.copy()
print(phonebook3)

phonebook3.clear()
print(phonebook3)

print(phonebook2)                      # .copy() 的 “原件” 不会发生变化

p = phonebook1.popitem()
print(p)
print(phonebook1)

print(p)
print(phonebook1)

print(p)
print(phonebook1)

print(p)
print(phonebook1)``````
``````{'john': 9876, 'mike': 5603, 'stan': 6898, 'eric': 7898}
{}
{'john': 9876, 'mike': 5603, 'stan': 6898, 'eric': 7898}
('zoe', 1225)
{'ann': 6585, 'bob': 8982, 'joe': 2598}
3538
{'ann': 6585, 'bob': 8982, 'joe': 2598}
3538
{'ann': 6585, 'bob': 8982, 'joe': 2598}
3538
{'ann': 6585, 'bob': 8982, 'joe': 2598, 'adam': 3538}``````

## 迭代各种容器中的元素

``````for i in range(3):
print(i)``````
``````0
1
2``````
``````for i in [1, 2, 3]:
print(i)``````
``````1
2
3``````

### 迭代的同时获取索引

``````s = 'Python'
for i, c in enumerate(s):
print(i, c)``````
``````0 P
1 y
2 t
3 h
4 o
5 n``````
``````for i, v in enumerate(range(3)):
print(i, v)``````
``````0 0
1 1
2 2``````
``````L = ['ann', 'bob', 'joe', 'john', 'mike']
for i, L in enumerate(L):
print(i, L)``````
``````0 ann
1 bob
2 joe
3 john
4 mike``````
``````t = ('ann', 'bob', 'joe', 'john', 'mike')
for i, t in enumerate(t):
print(i, t)``````
``````0 ann
1 bob
2 joe
3 john
4 mike``````

### 迭代前排序

``````t = ('bob', 'ann', 'john', 'mike', 'joe')
for i, t in enumerate(sorted(t)):
print(i, t)``````
``````0 ann
1 bob
2 joe
3 john
4 mike``````
``````t = ('bob', 'ann', 'john', 'mike', 'joe')
for i, t in enumerate(sorted(t, reverse=True)):
print(i, t)``````
``````0 mike
1 john
2 joe
3 bob
4 ann``````
``````t = ('bob', 'ann', 'john', 'mike', 'joe')
for i, t in enumerate(reversed(t)):
print(i, t)``````
``````0 joe
1 mike
2 john
3 ann
4 bob``````

### 同时迭代多个容器

``````chars = 'abcdefghijklmnopqrstuvwxyz'
nums = range(1, 27)
for c, n in zip(chars, nums):
print(f"Let's assume {c} represents {n}.")``````
``````Let's assume a represents 1.
Let's assume b represents 2.
Let's assume c represents 3.
Let's assume d represents 4.
Let's assume e represents 5.
Let's assume f represents 6.
Let's assume g represents 7.
Let's assume h represents 8.
Let's assume i represents 9.
Let's assume j represents 10.
Let's assume k represents 11.
Let's assume l represents 12.
Let's assume m represents 13.
Let's assume n represents 14.
Let's assume o represents 15.
Let's assume p represents 16.
Let's assume q represents 17.
Let's assume r represents 18.
Let's assume s represents 19.
Let's assume t represents 20.
Let's assume u represents 21.
Let's assume v represents 22.
Let's assume w represents 23.
Let's assume x represents 24.
Let's assume y represents 25.
Let's assume z represents 26.``````

### 迭代字典中的元素

``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}

for key in phonebook1:
print(key, phonebook1[key])``````
``````ann 6585
bob 8982
joe 2598
zoe 1225``````
``````phonebook1 = {'ann':6575, 'bob':8982, 'joe':2598, 'zoe':1225, 'ann':6585}

for key, value in phonebook1.items():
print(key, value)``````
``````ann 6585
bob 8982
joe 2598
zoe 1225``````