03-Python的数据类型3(字典)
# 字典
- 字典的定义
- 字典是一种无序的映射集合,不是序列,常用花括号表示,包含一系列的“键:值“对。
- 字典是无序的,它通过键索引映射的值,而不是通过位置来索引。
- 字典中的值并没有特殊的顺序,但是都存储在一个特定的键(key)里。
- 字典的键通常采用字符串,但也可以使用数字、元组等不可变的类型。
- 字典的值可以是任意类型。
- 字典长度可变,可以任意嵌套。
- 字典存储的是对象的引用,而不是对象本身。
- 字典的基本操作
- 创建字典
# 1. 创建空字典
# 方式1
d = {}
print(d,type(d))
# 方式2
d = dict()
print(d, type(d))
# 2. 创建有值的字典
# 使用 = 创建,也意味这也可以修改值
d = {"one":1,"two":2,"three":3}
print(d)
# 使用dict创建
d = ({"one":1,"two":2,"three":3})
print(d)
# 结果
{} <class 'dict'>
{} <class 'dict'>
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
# 3.利用赋值格式的键值对创建字典
d = dict(one=1,two=2,three=3)
print(d)
# 4.使用包含键元组和值元组的列表创建字典
d = dict([("one",1),("two",2),("three",3)])
print(d)
# 5.使用zip解析键值列表创建字典
d = dict(zip(['name','age'],['Xiaoh','18']))
print(d)
# 结果
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'name': 'Xiaoh', 'age': '18'}
{'name': None, 'age': None, 'sex': None}
# 6.创建无映射值的字典,默认值为None
d = dict.fromkeys(['name', 'age'])
print(d)
d = dict.fromkeys('abc')
print(d)
# 7.创建映射值相同的字典。
d = dict.fromkeys('abc', 15)
print(d)
# 8.通过赋值添加”键:值“对
x = {}
x['name'] = 'abc'
x['age'] = 18
print(x)
# 结果
{'name': None, 'age': None}
{'a': None, 'b': None, 'c': None}
{'a': 15, 'b': 15, 'c': 15}
{'name': 'abc', 'age': 18}
- 删除字典
# 1.使用del
d = ({"one":1,"two":2,"three":3})
del d["one"]
print(d)
# 使用clear()
d.clear()
print(d)
# 结果
{'two': 2, 'three': 3}
{}
- 访问字典
d = ({"one":1,"two":2,"three":3})
print(d['one'])
# 结果
1
- 字典遍历
d = ({"one":1,"two":2,"three":3})
# 使用for 循环
# 第一种
for k in d:
print(k, d[k])
# 第二种
for k in d.keys():
print(k, d[k])
# 只访问值
for v in d.values():
print(v)
# 借用items()
for k,v in d.items():
print(k,":",v)
# 结果
one 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one : 1
two : 2
three : 3
- 字典的常用方法(函数)
- cmp(dict1, dict2)
* 比较两个字典元素。
- len(dict)
* 计算字典元素个数,即键的总数。
- str(dict)
* 输出字典可打印的字符串表示。
- dict.has_key(key)
* 如果键在字典dict里返回true,否则返回false
- dict.items()
* 以列表返回可遍历的(键, 值) 元组数组
- dict.keys()
* 以列表返回一个字典所有的键
- dict.values()
* 以列表返回字典中的所有值
- type(variable)
* 返回输入的变量类型,如果变量是字典就返回字典类型。
- dict.clear()
* 删除字典内所有元素
- dict.fromkeys(seq[, val])
* 创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值
- dict.copy()
* 返回一个字典的浅复制
# 复制字典对象
d = ({"one":1,"two":2,"three":3})
x = d
print(x, d)
d["two"] = 40
print(x, d)
print(x is d)
# 使用copy()
y = d.copy()
print(y, d)
d["two"] = 50
print(y, d)
print(y is d)
# 结果
{'one': 1, 'two': 2, 'three': 3} {'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 40, 'three': 3} {'one': 1, 'two': 40, 'three': 3}
True
{'one': 1, 'two': 40, 'three': 3} {'one': 1, 'two': 40, 'three': 3}
{'one': 1, 'two': 40, 'three': 3} {'one': 1, 'two': 50, 'three': 3}
False
- dict.get(key, default=None)
* 返回指定键的值,如果值不在字典中返回default值
d = ({"one":1,"two":2,"three":3})
print(d.get("one")) # 返回映射值
print(d.get("four")) # 不存在返回空值
print(d.get("four","225")) # 不存在返回指定值
# 结果
1
None
225
- dict.setdefault(key, default=None
* 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default
- dict.update(dict2)
* 把字典dict2的键/值对更新到dict里
# setdefault(key[,default])
d = ({"one":1,"two":2,"three":3})
print(d.setdefault('one'))# 返回指定键的映射值
d.setdefault('four')
print(d) # 键不存在,添加默认值键值对
print(d.setdefault('five','333'))
print(d)# 添加键值对
print('--'*25)
# update(other)
d = ({"one":1,"two":2,"three":3})
print(d.update({'one':10, 'four':4}))
print(d)
# 结果
1
{'one': 1, 'two': 2, 'three': 3, 'four': None}
333
{'one': 1, 'two': 2, 'three': 3, 'four': None, 'five': '333'}
--------------------------------------------------
None
{'one': 10, 'two': 2, 'three': 3, 'four': 4}
- pop(key[,default])
* 删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
- popitem()
* 随机返回并删除字典中的一对键和值。
# pop()
d = ({"one":1,"two":2,"three":3})
print(d.pop('one'))
print(d.pop('four',"不存在这个键"))
print('*'*25)
# popitem()
d = ({"one":1,"two":2,"three":3})
print(d.popitem())
print(d)
# 结果
1
不存在这个键
*************************
('three', 3)
{'one': 1, 'two': 2}

浙公网安备 33010602011771号