字典

%字典用keys来连接values
1
x = {'Christopher Brooks': 'brooksch@umich.edu', 'Bill Gates': 'billg@microsoft.com'} 2 x['Christopher Brooks'] # Retrieve a value by using the indexing operator
'brooksch@umich.edu'

1 x['Kevyn Collins-Thompson'] = None
2 x['Kevyn Collins-Thompson']
None

%迭代遍历所有的keys
1
for name in x: 2 print(x[name])
brooksch@umich.edu
billg@microsoft.com
None

%迭代遍历所有的值
1
for email in x.values(): 2 print(email)
brooksch@umich.edu
billg@microsoft.com
None

%迭代在列表中的所有项
1
for email, name in x.items(): 2 print(name) 3 print(email)
brooksch@umich.edu
Christopher Brooks
billg@microsoft.com
Bill Gates
None
Kevyn Collins-Thompson




原文地址:https://www.cnblogs.com/zhengzhe/p/8510837.html