1. 字典
1. 语法
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
dicN2 = {}
dicN3 = dict()
2. 常见操作
1. 增
- 语法
字典[Key] = Value
- 示例
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1)
dicN1["id"] = 0
print(dicN1)
dicN1["name"] = "ccblogs1"
print(dicN1)
{'name': 'ccblogs', 'age': 18, 'gender': '0'}
{'name': 'ccblogs', 'age': 18, 'gender': '0', 'id': 0}
{'name': 'ccblogs1', 'age': 18, 'gender': '0', 'id': 0}
2. 删
- del()/del:删除字典或删除字典中指定键值对。
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1)
del dicN1["gender"]
print(dicN1)
{'name': 'ccblogs', 'age': 18, 'gender': '0'}
{'name': 'ccblogs', 'age': 18}
- clear():清空字典
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1)
dicN1.clear()
print(dicN1)
{'name': 'ccblogs', 'age': 18, 'gender': '0'}
{}
3. 查找
- 按Key查找
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1["age"])
18
- get()
字典.get(Key, 默认值)
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1.get("age"))
print(dicN1.get("age1", 20))
print(dicN1.get("age1"))
18
20
None
- keys()
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1.keys())
dict_keys(['name', 'age', 'gender'])
- values()
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1.values())
dict_values(['ccblogs', 18, '0'])
- items()
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
print(dicN1.items())
dict_items([('name', 'ccblogs'), ('age', 18), ('gender', '0')])
4. 遍历
- 遍历Key
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
for key in dicN1.keys():
print(key)
name
age
gender
- 遍历value
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
for value in dicN1.values():
print(value)
ccblogs
18
0
- 遍历字典的元素
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
for item in dicN1.items():
print(item)
('name', 'ccblogs')
('age', 18)
('gender', '0')
- 遍历字典的键值对
dicN1 = {"name":"ccblogs", "age":18, "gender":"0"}
for key, value in dicN1.items():
print(f"{key} = {value}")
name = ccblogs
age = 18
gender = 0
2. 集合
1. 语法
setNum1 = {1, 2, 3, 4, 5, 6}
print(setNum1)
setNum2 = {1, 2, 3, 4, 5, 6, 4, 3}
print(setNum2)
setStr1 = set("abcdefg")
print(setStr1)
setStr2 = set()
print(type(setStr2))
setStr3 = {}
print(type(setStr3))
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
{'b', 'f', 'a', 'd', 'g', 'e', 'c'}
2. 常见操作方法
1. 增
- add():增加单一数据
setNum1 = {1, 2, 3, 4, 5, 6}
setNum1.add(7)
setNum1.add(1)
print(setNum1)
{1, 2, 3, 4, 5, 6, 7}
- update():增加序列
setNum1 = {1, 2, 3, 4, 5, 6}
setNum1.update([7, 8, 2, 5])
print(setNum1)
{1, 2, 3, 4, 5, 6, 7, 8}
2. 删除
- remove():删除集合中的指定数据,如果数据不存在则报错
setNum1 = {1, 2, 3, 4, 5, 6}
setNum1.remove(5)
print(setNum1)
{1, 2, 3, 4, 6}
- discard():删除集合中的指定数据,如果数据不存在也不会报错。
setNum1 = {1, 2, 3, 4, 5, 6}
setNum1.discard(5)
print(setNum1)
{1, 2, 3, 4, 6}
- pop():随机删除一个数据并返回这个数据。
setNum1 = {1, 2, 3, 4, 5, 6}
num = setNum1.pop()
print(num)
print(setNum1)
1
{2, 3, 4, 5, 6}
3. 查找
- in:判断数据在集合序列
setNum1 = {1, 2, 3, 4, 5, 6}
print(1 in setNum1)
print(7 in setNum1)
True
False
- not in:判断数据不在集合序列
setNum1 = {1, 2, 3, 4, 5, 6}
print(1 not in setNum1)
print(7 not in setNum1)
False
True