python 字典

1. 字典的背景

names = ['Alice', 'Beth', 'Cecil', 'De-De']
numbers = [1, 2, 3, 4]
print(numbers[names.index('Beth')])
========================1.========================
2

2. 创建和使用字典

phonebook = {
    'Alice': 1,
    'Beth': 2,
    'Cecil': 3,
    'De-De': 4
}
print(phonebook['Beth'])
========================2.========================
2

2. 创建和使用字典

phonebook = {
    'Alice': 1,
    'Beth': 2,
    'Cecil': 3,
    'De-De': 4
}
print(phonebook['Beth'])

========================2.========================
2

2.2.1 使用dict类来创建

# 转换其他映射
items = [('name', 'Gumby'), ('age', 24)]
d = dict(items)
print(d['name'])

# 传递关键字实参
d = dict(name='Gumby', age=24)
print(d['age'])
======================2.2.1=======================
Gumby
24

2.2.2 字典的基本操作

# len 字典项数
temp = len(d)
print(temp)

# 返回指定键对应的值
temp = d['name']
print(temp)

# 将值关联到指定的键
d['gender'] = True
print(d)

# 删指定键对应的项
del d['gender']
print(d)

# 检查字典是否包含指定键的项
print('name' in d)
print('gender' in d)

# 键值可以是任何不可变的类型 实数,字符串,元组
d[3.1] = '实数'
d['3.1'] = '字符串'
d[(3, 1)] = '元组'
print(d)
======================2.2.2=======================
2
Gumby
{'name': 'Gumby', 'age': 24, 'gender': True}
{'name': 'Gumby', 'age': 24}
True
False
{'name': 'Gumby', 'age': 24, 3.1: '实数', '3.1': '字符串', (3, 1): '元组'}

2.2.3 字符串格式化带字典

phonebook = {'alice': '111', 'herry': '222', 'zenos': '333'}
format_str = "alice's phonenumber is {alice}".format_map(phonebook)
print(format_str)
======================2.2.3=======================
alice's phonenumber is 111

2.2.4 字典方法

print('2.2.4.1'.center(50, '='))
# 2.2.4.1 clear 删除所有字典的项
d1 = {'name': 'alice'}
d1.clear()
print(d1)

print('2.2.4.2'.center(50, '='))
# 2.2.4.2 copy 浅复制  键为新键,值仍然为原件的值
d1 = {
    'name': 'alice',
    'students': ['zenos', 'herry']
}
d2 = d1.copy()
print(d2)
# 对副本的值进行替换, 原件不受影响
d2['name'] = 'zenos'
print(d2)
print(d1)
# 对副本的值进行修改, 原件受影响
d2['students'].remove('zenos')
print(d2)
print(d1)

print('2.2.4.3'.center(50, '='))
# 2.2.4.3 deepcopy 深拷贝
from copy import deepcopy
d1 = {
    'name': 'alice',
    'students': ['zenos', 'herry']
}
d2 = deepcopy(d1)
print(d2)
# 对副本的值进行替换, 原件不受影响
d2['name'] = 'zenos'
print(d2)
print(d1)
# 对副本的值进行修改, 原件不受影响
d2['students'].remove('zenos')
print(d2)
print(d1)

print('2.2.4.4'.center(50, '='))
# 2.2.4.4 fromkeys 使用列表创建字典
d1 = dict.fromkeys(['name', 'age'], 'zenos')
print(d1) 

print('2.2.4.5'.center(50, '='))
# 2.2.4.5 get 访问字典, 没有存在的项就返回默认值
gender = d1.get('gender', 'N/A')
print(gender)

print('2.2.4.6'.center(50, '='))
# 2.2.4.6 items 返回所有字典项的列表
print(d1.items())

print('2.2.4.7'.center(50, '='))
# 2.2.4.7 keys 返回字典键视图
print(d1.keys())

print('2.2.4.8'.center(50, '='))
# 2.2.4.8 values 返回字典值视图
print(d1.keys())

print('2.2.4.9'.center(50, '='))
# 2.2.4.9 pop 获取指定的键的关联的值, 并删除这个项
name = d1.pop('name')
print(name)
print(d1)

print('2.2.4.10'.center(50, '='))
# 2.2.4.10 popitem 弹出最后一个字典项
print(d)
print(d.popitem())

print('2.2.4.11'.center(50, '='))
# 2.2.4.11 setdefault 访问字典, 没有存在的项就设置默认值
print(d1)
name = d1.setdefault('name', 'zenos')
print(name)
print(d1)

print('2.2.4.12'.center(50, '='))
# 2.2.4.12 update 使用一个字典的更新另外一个字典
d = {
    'title': 'python web site',
    'url': 'www.python.org',
    'changed': 'Mar 14 22:09:15 MET 2016'
}
x = {
    'title': 'Python Language Website'
}
d.update(x)
print(d)

=====================2.2.4.1======================
{}
=====================2.2.4.2======================
{'name': 'alice', 'students': ['zenos', 'herry']}
{'name': 'zenos', 'students': ['zenos', 'herry']}
{'name': 'alice', 'students': ['zenos', 'herry']}
{'name': 'zenos', 'students': ['herry']}
{'name': 'alice', 'students': ['herry']}
=====================2.2.4.3======================
{'name': 'alice', 'students': ['zenos', 'herry']}
{'name': 'zenos', 'students': ['zenos', 'herry']}
{'name': 'alice', 'students': ['zenos', 'herry']}
{'name': 'zenos', 'students': ['herry']}
{'name': 'alice', 'students': ['zenos', 'herry']}
=====================2.2.4.4======================
{'name': 'zenos', 'age': 'zenos'}
=====================2.2.4.5======================
N/A
=====================2.2.4.6======================
dict_items([('name', 'zenos'), ('age', 'zenos')])
=====================2.2.4.7======================
dict_keys(['name', 'age'])
=====================2.2.4.8======================
dict_keys(['name', 'age'])
=====================2.2.4.9======================
zenos
{'age': 'zenos'}
=====================2.2.4.10=====================
{'name': 'Gumby', 'age': 24, 3.1: '实数', '3.1': '字符串', (3, 1): '元组'}
((3, 1), '元组')
=====================2.2.4.11=====================
{'age': 'zenos'}
zenos
{'age': 'zenos', 'name': 'zenos'}
=====================2.2.4.12=====================
{'title': 'Python Language Website', 'url': 'www.python.org', 'changed': 'Mar 14 22:09:15 MET 2016'}

你可能感兴趣的:(python 字典)