创建字典后的数据格式
字典里没有顺序的概念
{'姓名':'张三'}
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞
', 30: '赞', 31: '赞'}
创建字典
dictStr = {1:'one', 2:'two', 3:'three', 4:'four'}
print(dictStr)
# 打印
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
# 访问对应的值
print(dictStr[2])
# 打印
two
创建空的字典
dictStr = {}
print(dictStr)
# 打印
{}
实现索引
jsondata = {'name': '张三', 'age':18}
print(jsondata['name'])
#打印
张三
brand = ['张三', '李四', '王五', '李白']
age = ['18', '20', '28', '10']
print(age[brand.index('张三')])
# 打印
18
创建字典 dict()
元组的方式
##### 这里使用的是元组的方式
dictStr = dict(((1,'one'),(2,'two'),(3,'three'),(4,'four')))
print(dictStr)
# 打印
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
列表的方式
dictStr2 = dict(([1,'one'],[2,'two'],[3,'three'],[4,'four']))
print(dictStr2)
# 打印
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
关键字的方式
dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
print(dictStr3['张三'])
# 打印
你好世界
修改键值
dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
dictStr2['张三'] = 'abcd'
print(dictStr3['张三'])
# 打印
abcd
新增键值
dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
dictStr2['李四'] = '飞流直下三千尺'
print(dictStr3)
# 打印
{'张三': '你好世界', '苍井空': '让av征服全世界', '李四': '飞流直下三千尺'}
访问字典 .fromkeys()
dict1 = {} # 初始化一个字典
dict1 = dict1.fromkeys((1, 2, 3))
dict1 = dict1.fromkeys((1, 2, 3), 'Number')
# 打印
{1: None, 2: None, 3: None}
{1: 'Number', 2: 'Number', 3: 'Number'}
- keys() 打印 索引值
- values() 打印value的值
- items() 整个元素打印出现 ```python dict1 = {} # 初始化一个字典
dict1 = dict1.fromkeys(range(32), ‘赞’) print(dict1)
打印
{0: ‘赞’, 1: ‘赞’, 2: ‘赞’, 3: ‘赞’, 4: ‘赞’, 5: ‘赞’, 6: ‘赞’, 7: ‘赞’, 8: ‘赞’, 9: ‘赞’, 10: ‘赞’, 11: ‘赞’, 12: ‘赞’, 13: ‘赞’, 14: ‘赞’, 15: ‘赞’, 16: ‘赞’, 17: ‘赞’, 18: ‘赞’, 19: ‘赞’, 20: ‘赞’, 21: ‘赞’, 22: ‘赞’, 23: ‘赞’, 24: ‘赞’, 25: ‘赞’, 26: ‘赞’, 27: ‘赞’, 28: ‘赞’, 29: ‘赞 ‘, 30: ‘赞’, 31: ‘赞’}
for eachkey in dict1.keys(): print(eachkey) # 打印: 0 1 2
for eachvalue in dict1.values(): print(eachvalue) # 打印: 赞 赞 赞
for eachitem in dict1.items(): print(eachitem) # 打印: (0, ‘赞’) (1, ‘赞’)
<a name="j4Abj"></a>
### 访问超出下标值 .get()
```python
dict1 = dict1.fromkeys(range(32), '赞')
print(dict1[31]) # 0 ~ 31 # 显示正常 赞
print(dict1[32]) # 报错 KeyError: 32
print(dict1.get(32)) # None
print(dict1.get(32, '木有!')) # 如果有值的话就会打印值, 如果没有的话会打印 木有!
print(dict1.get(31, '木有!')) # 打印 赞
返回最后一个值 .popitem()
a = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
print(a.popitem())
# 打印
4: 'four'
查找键,没有就添加键 .setdefault()
查找键, 如果查找的键不存在就会插入该查找的键
a = {1: 'one', 2: 'two', 3: 'three'}
print(a.setdefault(3))
print(a.setdefault('张三'))
print(a.setdefault(4,'张三'))
# 打印
three
{1: 'one', 2: 'two', 3: 'three', '张三': None}
{1: 'one', 2: 'two', 3: 'three', 4: '张三'}
清空字典 .clear()
dict1 = dict1.fromkeys(range(32), '赞')
dict1.clear()
# 打印
None
删除值 .pop()
参数 给一个键 返回删除对应的值
a = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
print(a.pop(1))
# 打印
one
更新 .update()
a = {1: 'one', 2: 'two', 3: 'three'}
print(a)
b = {3: '李四'}
a.update(b)
print(a)
# 打印
{1: 'one', 2: 'two', 3: 'three'}
{1: 'one', 2: 'two', 3: '李四'}