python基础语法(4) 字典
2017-01-08 21:18
393 查看
字典用{ }表示。。 表示mapping的关心。
例如: phonebook ={'Alice':'2314' , 'Beth': '9102' , 'Cecil':'3258' }
字典和列表的不同
x = []
x[42] = 'Footbar'
Traceback (most recent call last):
File " <stdin>" . line 1 , in?
IndexError: list assigment index out of range
x = {}
x[42] = 'Footbar'
x
{42: 'Foobar'}
字典应用举例
people = {
'Alice' : {
'phone':'2341',
'addr':'Foo drive 23'
},
'Beth':{
'phone':'9102',
'addr': 'Bar street 42'
}
'Ceil':{
'phone':'3158'
'addr':'Baz avenue 90'
}
# 针对电话号码和地址使用的描述性标签, 会在打印输出的时候用到
lables = {
'phone': 'phone number',
'addr': 'address'
}
name = raw_input('Phone number (p) or address (a)?')
if request == 'p' : key = 'phone'
if request == 'a' : key = 'addr'
if name in people: print "%s's %s is %s. " %\
(name. lables[key],people[name][key])
python 中所有的索引符都是中括号。
}
例如: phonebook ={'Alice':'2314' , 'Beth': '9102' , 'Cecil':'3258' }
字典和列表的不同
x = []
x[42] = 'Footbar'
Traceback (most recent call last):
File " <stdin>" . line 1 , in?
IndexError: list assigment index out of range
x = {}
x[42] = 'Footbar'
x
{42: 'Foobar'}
字典应用举例
people = {
'Alice' : {
'phone':'2341',
'addr':'Foo drive 23'
},
'Beth':{
'phone':'9102',
'addr': 'Bar street 42'
}
'Ceil':{
'phone':'3158'
'addr':'Baz avenue 90'
}
# 针对电话号码和地址使用的描述性标签, 会在打印输出的时候用到
lables = {
'phone': 'phone number',
'addr': 'address'
}
name = raw_input('Phone number (p) or address (a)?')
if request == 'p' : key = 'phone'
if request == 'a' : key = 'addr'
if name in people: print "%s's %s is %s. " %\
(name. lables[key],people[name][key])
python 中所有的索引符都是中括号。
}
相关文章推荐
- python爬虫基础
- 459. Repeated Substring Pattern
- Python基础知识
- 同时安装python2.x和python3.x用pip
- 用户房源推荐—基于内容的推荐算法(CB)
- 【实验小结】cs231n assignment1 knn 部分
- 深刻理解Python中的元类(metaclass)
- Python自动化之session
- numpy基本矩阵操作
- Python 库大全
- Jupyter notebook使用记录
- Numpy处理数据
- Python编写的客户端给服务器发送指令执行相应的命令并返回结果
- SVM手写数字的识别---python
- python-opencv-特征点匹配连线(画线)drawMatches
- python往mysql中插入datetime类型的数据
- Pip源
- NumPy的详细教程
- python 多线程-threading模块
- python windows扩展包