Python学习笔记——字典

1. 用法

#列表
brand = ['李宁', '耐克', '阿迪达斯', '鱼C工作室']
slogan = ['一切皆有可能', 'Just do it', 'Impossible is nothing', '让编程改变世界']
print('李宁的口号是:', slogan[brand.index('李宁')])
李宁的口号是: 一切皆有可能
#字典(映射类型)
dict1 = {'李宁':'一切皆有可能', '耐克':'Just do it', '阿迪达斯':'Impossible is nothing', '鱼C工作室':'让编程改变世界'}
print('李宁的口号是:',dict1['李宁'])
李宁的口号是: 一切皆有可能

2. 用dict()创建字典

dict3 = dict(((1,'one'),(2,'two'),(3,'three')))
print(dict3)
{1: 'one', 2: 'two', 3: 'three'}
#关键字形式
dict4 = dict(一='one',二='two',三='three')#此处关键字不能加引号,也不能为数字,否则报错
print(dict4)
{'一': 'one', '二': 'two', '三': 'three'}
#采用下列语句:如果原字典中未出现此关键字,则新建;若已有,则改变他的值
dict4['爱迪生'] = '爱迪生的天才论'
print(dict4)
{'一': 'one', '二': 'two', '三': 'three', '爱迪生': '爱迪生的天才论'}

3. fromkeys()

#创建并返回一个新的字典
dict1 = {}
print(dict1.fromkeys((1,2,3)))
print(dict1)
{1: None, 2: None, 3: None}
{}
print(dict1.fromkeys((1,2,3),'number'))
{1: 'number', 2: 'number', 3: 'number'}
print(dict1.fromkeys((1,2,3),('one','two','three')))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

4. keys()、values() 和 items()

dict2 = dict1.fromkeys(range(4),'赞')
for eachkey in dict2.keys():
    print(eachkey)
0
1
2
3
for eachvalue in dict2.values():
    print(eachvalue)
赞
赞
赞
赞
for eachitem in dict2.items():
    print(eachitem)
(0, '赞')
(1, '赞')
(2, '赞')
(3, '赞')

5. get()

#可以获取相应关键字对应的值,如果没有此关键字,则返回None
print(dict2.get(4))
None
#如果该关键字存在,则返回他对应的值,若不存在,则返回自己设定的值
print(dict2.get(3,'字典中不存在此关键字'))
print(dict2.get(4,'字典中不存在此关键字'))
赞
字典中不存在此关键字

6. in 和 not in

print(4 in dict2)
print(3 in dict2)
False
True

7. clear()

#清空字典
dict2.clear()
print(dict2)
{}
# 若用 = {} 的方式清空字典的话,若其已被赋给其他变量,则那个变量未清空
# 会造成隐患
a = {'name':'Nigream'}
b = a
a[2] = 'hh'
a = {}
print(a)
print(b)
{}
{'name': 'Nigream', 2: 'hh'}

# 使用clear则不会出现以上问题
c = {'name':'Nigream'}
d = c
c.clear()
print(c)
print(d)
{}
{}

8. 拷贝

e = {1:'one',2:'two',3:'three'}
f = e.copy()
g = e
print(id(e),e)
print(id(g),g)
print(id(f),f)
# g可以理解为引用变量,与e指向同一地址空间
# 而f为e的一个拷贝,在计算机内创建了新的空间
2392126414568 {1: 'one', 2: 'two', 3: 'three'}
2392126414568 {1: 'one', 2: 'two', 3: 'three'}
2392126342992 {1: 'one', 2: 'two', 3: 'three'}

e[4] = 'four'
print(id(e),e)
print(id(g),g)
print(id(f),f)
# 这里修改e,未对f造成影响
2392126414568 {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
2392126414568 {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
2392126342992 {1: 'one', 2: 'two', 3: 'three'}

9. pop()

e = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
# 此处会取出指定关键字的项目,并返回对应的键值
print(e.pop(2))
print(e)
e = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
two
{1: 'one', 3: 'three', 4: 'four'}

e = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
# 此处会取出最后一个项目,并返回对应的键和键值
print(e.popitem())
print(e)
e = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
(4, 'four')
{1: 'one', 2: 'two', 3: 'three'}

10. setdefault() 和 update()

e.setdefault('小白')
print(e)
# 用来加入新键值对,若未指定键值,则默认为None
{1: 'one', 2: 'two', 3: 'three', 4: 'four', '小白': None}

e.setdefault(5,'five')
print(e)
# 用来加入新键值对
{1: 'one', 2: 'two', 3: 'three', 4: 'four', '小白': None, 5: 'five'}

e.setdefault('小白','狗')
print(e)
# 若键已存在,则无变化
{1: 'one', 2: 'two', 3: 'three', 4: 'four', '小白': None, 5: 'five'}

# 可以用update修改已有的键值
e.update({'小白':'狗'})#注意此处有大括号
print(e)
{1: 'one', 2: 'two', 3: 'three', 4: 'four', '小白': '狗', 5: 'five'}

posted @ 2019-07-26 16:20  nigream  阅读(123)  评论(0编辑  收藏  举报