Python入门手册:Python中的数据结构范例

打印 上一主题 下一主题

主题 2104|帖子 2104|积分 6312


在Python中,数据结构是组织和存储数据的方式,它们答应你以高效的方式操作和处置处罚数据。Python提供了几种内置的数据结构,包罗列表(List)、元组(Tuple)、集合(Set)和字典(Dictionary)。这些数据结构各有特点,实用于不同的场景。本文将具体介绍这些数据结构的定义、操作和使用方法,帮助你更好地理解和应用它们。
1. 列表(List)

列表是一种有序的集合,可以包含不同范例的元素。列表是可变的,这意味着你可以在列表中添加、删除或修改元素。
定义列表

  1. # 定义列表
  2. fruits = ["apple", "banana", "cherry"]
  3. numbers = [1, 2, 3, 4, 5]
  4. mixed_list = [1, "hello", 3.14, True]
复制代码
访问列表元素

  1. # 访问列表元素
  2. print(fruits[0])  # 输出:apple
  3. print(numbers[2])  # 输出:3
复制代码
修改列表元素

  1. # 修改列表元素
  2. fruits[1] = "orange"
  3. print(fruits)  # 输出:['apple', 'orange', 'cherry']
复制代码
添加和删除元素

  1. # 添加元素
  2. fruits.append("grape")
  3. print(fruits)  # 输出:['apple', 'orange', 'cherry', 'grape']
  4. # 插入元素
  5. fruits.insert(1, "pear")
  6. print(fruits)  # 输出:['apple', 'pear', 'orange', 'cherry', 'grape']
  7. # 删除元素
  8. del fruits[1]
  9. print(fruits)  # 输出:['apple', 'orange', 'cherry', 'grape']
  10. # 移除元素
  11. fruits.remove("orange")
  12. print(fruits)  # 输出:['apple', 'cherry', 'grape']
复制代码
列表切片

  1. # 列表切片
  2. numbers = [1, 2, 3, 4, 5]
  3. print(numbers[1:3])  # 输出:[2, 3]
  4. print(numbers[:3])   # 输出:[1, 2, 3]
  5. print(numbers[3:])   # 输出:[4, 5]
复制代码
2. 元组(Tuple)

元组是一种有序的集合,与列表类似,但元组是不可变的,这意味着一旦创建,元组中的元素不能被修改。
定义元组

  1. # 定义元组
  2. fruits = ("apple", "banana", "cherry")
  3. numbers = (1, 2, 3, 4, 5)
复制代码
访问元组元素

  1. # 访问元组元素
  2. print(fruits[0])  # 输出:apple
  3. print(numbers[2])  # 输出:3
复制代码
元组切片

  1. # 元组切片
  2. numbers = (1, 2, 3, 4, 5)
  3. print(numbers[1:3])  # 输出:(2, 3)
  4. print(numbers[:3])   # 输出:(1, 2, 3)
  5. print(numbers[3:])   # 输出:(4, 5)
复制代码
3. 集合(Set)

集合是一种无序的集合,不包含重复的元素。集合支持数学上的集合操作,如并集、交集和差集。
定义集合

  1. # 定义集合
  2. fruits = {"apple", "banana", "cherry"}
  3. numbers = {1, 2, 3, 4, 5}
复制代码
添加和删除元素

  1. # 添加元素
  2. fruits.add("grape")
  3. print(fruits)  # 输出:{'apple', 'banana', 'cherry', 'grape'}
  4. # 删除元素
  5. fruits.remove("banana")
  6. print(fruits)  # 输出:{'apple', 'cherry', 'grape'}
复制代码
集合操作

  1. # 并集
  2. set1 = {1, 2, 3}
  3. set2 = {3, 4, 5}
  4. print(set1.union(set2))  # 输出:{1, 2, 3, 4, 5}
  5. # 交集
  6. print(set1.intersection(set2))  # 输出:{3}
  7. # 差集
  8. print(set1.difference(set2))  # 输出:{1, 2}
复制代码
4. 字典(Dictionary)

字典是一种无序的键值对集合,每个键必须是唯一的。字典是可变的,可以添加、删除或修改键值对。
定义字典

  1. # 定义字典
  2. person = {
  3.     "name": "Alice",
  4.     "age": 25,
  5.     "city": "New York"
  6. }
复制代码
访问字典元素

  1. # 访问字典元素
  2. print(person["name"])  # 输出:Alice
  3. print(person.get("age"))  # 输出:25
复制代码
添加和修改键值对

  1. # 添加键值对
  2. person["gender"] = "Female"
  3. print(person)  # 输出:{'name': 'Alice', 'age': 25, 'city': 'New York', 'gender': 'Female'}
  4. # 修改键值对
  5. person["age"] = 26
  6. print(person)  # 输出:{'name': 'Alice', 'age': 26, 'city': 'New York', 'gender': 'Female'}
复制代码
删除键值对

  1. # 删除键值对
  2. del person["city"]
  3. print(person)  # 输出:{'name': 'Alice', 'age': 26, 'gender': 'Female'}
复制代码
遍历字典

  1. # 遍历字典
  2. for key, value in person.items():
  3.     print(key, value)
  4. # 输出:
  5. # name Alice
  6. # age 26
  7. # gender Female
复制代码
总结

通过以上内容,我们具体介绍了Python的四种基本数据结构:列表、元组、集合和字典。每种数据结构都有其独特的特点和实用场景:


  • 列表:有序、可变,恰当存储和操作一系列元素。
  • 元组:有序、不可变,恰当存储不需要修改的数据。
  • 集合:无序、无重复,恰当进行集合操作。
  • 字典:无序、键值对,恰当存储和检索键值对数据。
掌握这些数据结构的定义、操作和使用方法,将帮助你在Python编程中更高效地处置处罚和组织数据。渴望本文能帮助你更好地理解和应用这些数据结构。如果你有任何疑问或需要进一步学习,接待随时交换探讨。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表