跳至内容

Python 字典

Python中,字典Dict是一个存储键值对的集合。

创建字典

Python中,通过将键值对 key:value 放入大括号 {},逗号分隔的方式创建集合。另外,也可以使用Python内置的 dict() 方法创建字典。

python
# 学号-名字键值对
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaoli", "103": "xiaoyu"}
# 学号-名字键值对
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaoli", "103": "xiaoyu"}

注意

字典的键必须是不可修改的类型,比如元组、字符串、整数值。不能使用可变量作为字典的键,比如列表。

字典长度

Python中,通过 len() 函数获取字典的键值对数量。

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print(len(students))


# 输出:
# 3
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print(len(students))


# 输出:
# 3

字典访问

Python中,通过 [key] 的方式读取字典的键值。也可以使用get()方法读取。

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print("学号101是", students["101"])
print("学号102是", students.get("102"))


# 输出:
# 学号101是xiaoming
# 学号102是xiaoli
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print("学号101是", students["101"])
print("学号102是", students.get("102"))


# 输出:
# 学号101是xiaoming
# 学号102是xiaoli

字典修改

Python字典是可变类型,我们可以通过 来修改 对应值

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

# 学号102的学生修改为xiaowang
students["102"] = "xiaowang"

print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaowang", "103": "xiaoyu"}
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

# 学号102的学生修改为xiaowang
students["102"] = "xiaowang"

print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaowang", "103": "xiaoyu"}

字典新增

Python中,通过 '[key]=value' 的方式新增键值对。也可以通过update()方法新增。

python
students = {
  "101": "xiaoming",
}

# 1. []方式新增
students["102"] = "xiaogao"
# 2. update()方法新增
students.update({"103":"xiaozhang"})
print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaogao", "103": "xiaozhang"}
students = {
  "101": "xiaoming",
}

# 1. []方式新增
students["102"] = "xiaogao"
# 2. update()方法新增
students.update({"103":"xiaozhang"})
print(students)


# 输出:
# {"101": "xiaoming", "102": "xiaogao", "103": "xiaozhang"}

字典删除

Python中,使用 del 语句删除字典项。也可以使用 pop() 方法删除。

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

# 1. del关键字删除
del students["103"]
# 2. pop方法删除
students.pop("102")

print(students)


# 输出:
# {"101": "xiaoming"}
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

# 1. del关键字删除
del students["103"]
# 2. pop方法删除
students.pop("102")

print(students)


# 输出:
# {"101": "xiaoming"}

如果需要一次全部删除字典项,可以使用 clear() 方法

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

students.clear()

print(students)


# 输出:
# {}
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

students.clear()

print(students)


# 输出:
# {}

字典查找

Python字典可以使用 in 或者 not in 操作符判断集合中主键是否存在

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print("101" in students)
print("102" not in students)
# 输出:
# True
# False
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

print("101" in students)
print("102" not in students)
# 输出:
# True
# False

注意

in 操作符只能检查键是否存在,不能检查值是否存在

字典遍历

Python字典通过for循环完成遍历。

python
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

for no in students:
    print(f'${no}:${students[no]}')


# 输出:
# "101": "xiaoming"
# "102": "xiaoli"
# "103": "xiaoyu"
students = {
  "101": "xiaoming",
  "102": "xiaoli",
  "103": "xiaoyu"
}

for no in students:
    print(f'${no}:${students[no]}')


# 输出:
# "101": "xiaoming"
# "102": "xiaoli"
# "103": "xiaoyu"

字典方法

函数名描述
pop()根据键删除字典项
update()新增或修改字典项
clear()删除所有字典项
keys()返回字典的所有键
values()返回字典的所有值
get()获取字典中指定键的值
popitem()以元组的形式返回最后插入的键值对
copy()返回字典的拷贝