关于人工智能:34-字典Dictionary

3次阅读

共计 2941 个字符,预计需要花费 8 分钟才能阅读完成。

字典(Dictionary)是 Python 中另一个十分有用的数据结构,它是一个无序的键值对(key-value pair)汇合。字典应用大括号 {} 示意,其中的键值对用冒号 : 分隔,键值对间用逗号 , 分隔。字典中的键必须是不可变类型,例如整数、浮点数、字符串、元组等。字典中的值能够是任意类型。

3.4.1 创立字典

创立字典的办法有两种:

  1. 应用大括号 {} 蕴含键值对,例如:
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person)
# 输入:{'name': 'Alice', 'age': 30, 'city': 'New York'}
  1. 应用 dict() 函数创立空字典或将其余序列转换为字典,例如:
empty_dict = dict()
print(empty_dict)  # 输入:{}

key_value_list = [("name", "Alice"), ("age", 30), ("city", "New York")]
person = dict(key_value_list)
print(person)
# 输入:{'name': 'Alice', 'age': 30, 'city': 'New York'}

3.4.2 拜访字典元素

拜访字典元素的办法有两种:

  1. 应用键作为下标,例如:
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person["name"])  # 输入:Alice

如果键不存在,会抛出 KeyError 异样。

  1. 应用 get() 办法,如果键不存在,返回一个默认值(可选),例如:
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person.get("name"))  # 输入:Alice
print(person.get("country", "USA"))  # 输入:USA

3.4.3 批改字典元素

批改字典元素的值,间接应用键作为下标并赋予新值,例如:

person = {"name": "Alice", "age": 30, "city": "New York"}
person["age"] = 31
print(person)
# 输入:{'name': 'Alice', 'age': 31, 'city': 'New York'}

3.4.4 增加和删除字典元素

  • 增加元素:应用不存在的键作为下标并赋予新值。
person = {"name": "Alice", "age": 30, "city": "New York"}
person["country"] = "USA"
print(person)
# 输入:{'name': 'Alice', 'age': 30, 'city': 'New York', 'country': 'USA'}
  • 删除元素:应用 del 语句和键作为下标。
person = {"name": "Alice", "age": 30, "city": "New York"}
del person["age"]
print(person)  # 输入:{'name': 'Alice', 'city': 'New York'}

如果键不存在,会抛出 KeyError 异样。

3.4.5 字典罕用办法

  • keys():返回字典中所有键的视图。
person = {"name": "Alice", "age": 30, "city": "New York"}
keys = person.keys()
print(keys)  # 输入:dict_keys(['name', 'age', 'city'])
  • values():返回字典中所有值的视图。
person = {"name": "Alice", "age": 30, "city": "New York"}
values = person.values()
print(values)  # 输入:dict_values(['Alice', 30, 'New York'])
  • items():返回字典中所有键值对的视图。
person = {"name": "Alice", "age": 30, "city": "New York"}
items = person.items()
print(items)

输入:
dict_items([(‘name’, ‘Alice’), (‘age’, 30), (‘city’, ‘New York’)])

  • update():用一个字典更新另一个字典,如果键曾经存在,则更新值;如果键不存在,则增加键值对。
person = {"name": "Alice", "age": 30, "city": "New York"}
new_info = {"age": 31, "country": "USA"}
person.update(new_info)
print(person)
# 输入:{'name': 'Alice', 'age': 31, 'city': 'New York', 'country': 'USA'}
  • pop():删除并返回指定键的值,如果键不存在,返回默认值(可选)。
person = {"name": "Alice", "age": 30, "city": "New York"}
age = person.pop("age")
print(age)  # 输入:30
print(person)  # 输入:{'name': 'Alice', 'city': 'New York'}
  • popitem():删除并返回字典中的一个键值对(Python3.7+,返回最初一个键值对;Python3.6 及之前版本,返回随机键值对)。
person = {"name": "Alice", "age": 30, "city": "New York"}
item = person.popitem()
print(item)  # 输入:('city', 'New York')
print(person)  # 输入:{'name': 'Alice', 'age': 30}
  • clear():删除字典中所有元素。
person = {"name": "Alice", "age": 30, "city": "New York"}
person.clear()
print(person)  # 输入:{}

3.4.6 遍历字典

遍历字典的办法有两种:

  1. 遍历键:
person = {"name": "Alice", "age": 30, "city": "New York"}
for key in person:
print(key, person[key])

或者应用 keys() 办法:

person = {"name": "Alice", "age": 30, "city": "New York"}
for key in person.keys():
print(key, person[key])
  1. 遍历键值对:
person = {"name": "Alice", "age": 30, "city": "New York"}
for key, value in person.items():
print(key, value)

心愿这些示例能帮忙你更好地了解和学习 Python 字典。如果有任何问题,请随时发问!
举荐浏览:

https://mp.weixin.qq.com/s/dV2JzXfgjDdCmWRmE0glDA

https://mp.weixin.qq.com/s/an83QZOWXHqll3SGPYTL5g

正文完
 0