# -*- coding:utf-8 -*-

# ------------------------------------
# ++  python字典:字典相当于 json  ++
# ------------------------------------

# 定义一个空字典:
dict = {}
print(type (dict))
print("空字典:",dict)

dict = {
    "name":"小花",
    "age":"23",
}
print("原始字典:",dict)

# 访问字典的值
print("我是",dict['name'])

# 访问没有的值: KeyError: 'error'
# print("出错了么?",dict['error'])

# 改:
dict['name'] = "我是花姑凉"
print(dict)

# 增:
dict["add"] = "addValue"
print(dict)

# 删:
'''
删除键
'''

del dict["name"]
print("删除键:",dict)

'''
清空字典
'''
dict.clear()
print("清空字典:",dict)

'''
删除字典
'''
del dict
print("删除字典:",dict)

'''
删除指定键,返回删除键的值
'''

dict  = {"name":"aaa"}
a = dict.pop("name")
print("删除键的值:",a)

# 字典键的特性:

'''
不允许同一个键出现两次,创建时如果同一个键被赋值两次,后一个值会被记住
'''
dict = {
    "name":"小花",
    "age":"23",
    "name":"小花猫",
}

print(dict)

'''
键必须不可变,可用数字,字符串,元组
'''

dict = {
    # ['name']:"小花",   # 不可以是列表
    ("name"):"小花花",
    "1":"我是数字",
    "age":"23",
}
print("可以么:",dict['name'])

# 字典的其他方法:len(dict)/str(dict)

'''
字典的长度
'''
print(len(dict))

'''
转化为字符串
'''
print(str(dict))

# 字典的遍历
for key,value in dict.items():
    print("我是键:"+key+",  我是值:"+value)

# 键值调换
reverse = {v: k for k,v in dict.items()}
print(reverse)

# 获取所有 keys
keys = dict.keys()

print(keys)

# 获取指定 key
print(list(keys)[0])