page contents

Python中有哪些常用的数据结构?

本文讲述了python中有哪些常用的数据结构?具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-11-6U0zBv4m6549d1dc4cb56.png本文讲述了python中有哪些常用的数据结构?具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

数字类型:Python 支持三种数字类型,分别是整数 int、浮点数 float 和复数 complex。

字符串类型:Python 的字符串 str 是不可变序列类型,用于表示文本信息。

布尔类型:Python 支持布尔值,它们是特殊的数字类型,只有两个取值,即 True 和 False。

列表类型:Python 的列表 list 是可变序列类型,可以保存多个任意类型的对象,并且支持多种操作。

元组类型:Python 的元组 tuple 是不可变序列类型,可以保存多个任意类型的对象,但一旦创建就不能修改。

字典类型:Python 的字典 dict 

堆栈是一种具有后进先出(LIFO)特性的数据结构,可以通过列表来实现。

集合(set)是一种无序不重复元素序列,可以进行数学运算,例如交集、并集等。

字典是另一种重要的数据结构,它以键-值对的形式存储数据,其中键是唯一的,而值可以是任何类型。

Python 还有一些其他特殊类型,如NoneType、Enum、Function、Module、Class 等

以下是代码示例,希望对你们学习有所帮助

数字类型

# 创建数字类型变量

int_num = 10

float_num = 3.14

complex_num = 3 + 4j

print(int_num, type(int_num))  # 输出:10

print(float_num, type(float_num))  # 输出:3.14

print(complex_num, type(complex_num))  # 输出:(3+4j)

字符串类型

str_text = "Hello world!"

# 使用索引访问字符

print(str_text[0], type(str_text[0]))  # 输出:H

# 使用切片访问子字符串

sub_str = str_text[7:12]

print(sub_str, type(sub_str))  # 输出:world!

布尔类型

bool_val1 = False

bool_val2 = not bool_val1

print(bool_val1, type(bool_val1))  # 输出:False

print(bool_val2, type(bool_val2))  # 输出:True

列表类型

list_items = [1, "two", 3.14, False]

# 插入元素

list_items.insert(2, 'new item')

# 按索引访问元素

print(list_items[2], type(list_items[2]))  # 输出:new item

# 按索引更新元素

list_items[0] = 2

print(list_items)  # 输出:[2, "two", "new item", False]

# 移除元素

list_items.pop(0)

print(list_items)  # 输出:["two", "new item", False]

元组类型

tuple_items = (1, "two", 3.14, False)

# 不能更改元组的内容

# tuple_items[0] = 2  # 这将引发错误!

# 按索引访问元素

print(tuple_items[1], type(tuple_items[1]))  # 输出:two

# 获取部分元素

part_tuple = tuple_items[1:]

print(part_tuple, type(part_tuple))  # 输出:("two", 3.14, False)

字典类型

dict_data = {"name": "John", "age": 30, "job": "developer"}

# 添加元素

dict_data["city"] = "New York"

# 按键访问元素

print(dict_data["name"], type(dict_data["name"]))  # 输出:John

# 更新元素

dict_data["age"] = 31

print(dict_data)  # 输出:{'name': 'John', 'age': 31, 'job': 'developer', 'city': 'New York'}

# 移除元素

del dict_data["job"]

print(dict_data)  # 输出:{'name': 'John', 'age': 31, 'city': 'New York'}

NoneType

# 创建一个 None 类型的变量

my_none = None

# 检查一个变量是否为 None

if my_none is None:

    print("The variable is None")

else:

    print("The variable is not None")

# 如果尝试访问 None 类型的变量会引发 TypeError

try:

    print(my_none.name)

except TypeError as e:

    print(e)  # 输出:"NoneType object has no attribute 'name'"

Enum

from enum import Enum

# 定义一个枚举类

class Color(Enum):

    RED = 1

    GREEN = 2

    BLUE = 3

# 创建一个枚举对象

my_color = Color.RED

# 访问枚举的名称和值

print(my_color.name)  # 输出:"RED"

print(my_color.value)  # 输出:1

Function

def greet(name):

    return f"Hello, {name}"

# 调用函数

greeting = greet("World")

print(greeting)  # 输出:"Hello, World"

# 函数也可以返回 None

def null_function():

    return None

result = null_function()

print(result)  # 输出:None

Module

# 导入模块

import math

# 使用模块中的函数

pi = math.pi

print(pi)  # 输出:3.141592653589793

# 使用模块中的常量

e = math.e

print(e)  # 输出:2.718281828459045

Class

class Person:

    def __init__(self, name, age):

        self.name = name

        self.age = age

    def say_hello(self):

        return f"Hello, my name is {self.name} and I am {self.age} years old."

# 创建一个对象

john = Person("John", 30)

# 访问对象的属性和方法

print(john.name)  # 输出:"John"

print(john.age)  # 输出:30

hello_message = john.say_hello()

print(hello_message)  # 输出:"Hello, my name is John and I am 30 years old."

堆栈(使用列表实现)

# 定义一个堆栈

stack = []

# 将元素添加到堆栈顶部

stack.append(1)

# 将另一个元素添加到堆栈顶部

stack.append('apple')

# 弹出堆栈顶部的元素

top_element = stack.pop()

print(top_element)  # 输出:'apple'

# 再次弹出堆栈顶部的元素

top_element = stack.pop()

print(top_element)  # 输出:1

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2023-11-07 13:58
  • 阅读 ( 186 )
  • 分类:Python开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章