|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
字典组成:
键key ;值value
字典或可称为hash table
字典是python中唯一一个映射类型:
映射类型要区别于之前讲过的序列类型
序列类型是以数组的方式来存储的,通过索引的方式获取相应位置的值,
索引值和对应元素的值是没有任何关系的
Bsp:
>>> brand=['lining','nike','adidas','fishc']
>>> slogan=['everything is possible','just do it','impossible is nothing','let programming change the world']
>>> print('fishc slogan is :',slogan[brand.index('fishc')])
fishc slogan is : let programming change the world
创建和访问字典:
>>> dict2={1:'one',2:'two',3:'three'}
>>> dict2[2]
'two'
创建字典的方式:
dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
Bsp:
1. dict3= dict((('F',70),('i',105),('s',115),('h',104),('c',99)))
>>> dict3
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'c': 99}
注意:字典只有一个参数,此处用元组将多个映射关系组成一个参数
2. 通过关键字和值创建字典
>>> dict4=dict(xiaojiayu='let programming change the world',cangjingkong='let AVconquer all the otaku ')
>>> dict4
{'xiaojiayu': 'let programming change the world', 'cangjingkong': 'let AVconquer all the otaku '}
注意:关键字不能加引号
3. 直接给字典的键赋值,若有此键则更改它的值,若不存在此键则创建新的键并为其赋值。与linux里的VI(打开一个文件的用法)比较相似
>>> dict4['cangjingkong']='all the AV works should learn to program'
>>> dict4
{'xiaojiayu': 'let programming change the world', 'cangjingkong': 'all the AV works should learn to program'}
>>> dict4['aidisheng']='genius is composed of 99 percent of hardwork and one percent of talent,but this 1 percent is more important than the 99% hardwork '
>>> dict4
{'xiaojiayu': 'let programming change the world', 'cangjingkong': 'all the AV works should learn to program', 'aidisheng': 'genius is composed of 99 percent of hardwork and one percent of talent,but this 1 percent is more important than the 99% hardwork '} |
评分
-
查看全部评分
|