数据结构篇
文章目录
1. 基本类型数组array.array
用于存储同一类型变量的容器,类似于数组。
from array import array
l = [1, 2, 3]
l_array = array('i', l)
print(l_array)
print(l_array.tolist())
print(l_array[0])
# print(l_array.append('s')) // TypeError
array('i', [1, 2, 3])
[1, 2, 3]
1
array.array支持list的方法,array的第一个参数用于规定容器中的数据类型,常用的类型如下:
| Type code | C Type | Python Type | Minimum size in bytes | Notes |
|---|---|---|---|---|
| ‘b’ | signed char | int | 1 | |
| ‘b’ | signed char | int | 1 | |
| ‘B’ | unsigned char | int | 1 | |
| ‘u’ | Py_UNICODE | Unicode character | 2 | (1) |
| ‘h’ | signed short | int | 2 | |
| ‘H’ | unsigned short | int | 2 | |
| ‘i’ | signed int | int | 2 | |
| ‘I’ | unsigned int | int | 2 | |
| ‘l’ | signed long | int | 4 | |
| ‘L’ | unsigned long | int | 4 | |
| ‘q’ | signed long | long int | 8 | (2) |
| ‘Q’ | unsigned long | long int | 8 | (2) |
| ‘f’ | float | float | 4 | |
| ‘d’ | double | float | 8 |
[参考文章](https://blog.youkuaiyun.com/pipisorry/article/details/62889137?ops_request_misc=&request_id=&biz_id=102&utm_term=python array.array&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-5-62889137.pc_search_ecpm_flag&spm=1018.2226.3001.4187)
2. 方便的数据对象-collection.namedtuple
与定义类相似,namedtuple支持声明具有字段名的数据。
from collections import namedtuple
from sys import getsizeof
class Car:
def __init__(self, type, color, price):
self.Type = type
self.Color = color
self.Price = price
def __repr__(self):
return f"Car(Type={self.Type}, Color={self.Color}, Price={self.Price})"
car1 = namedtuple('Car', 'Type Color Price')('奔驰', 'red', 1e5)
car2 = ('奥迪', 'black', 1e6)
print(car1)
print(car1.Type)
print(getsizeof(car1))
print(getsizeof(car2))
print(getsizeof(Car))
Car(Type='奔驰', Color='red', Price=100000.0) // nametuple具有不错的__repr__
奔驰
64 // 与相同类型的元组所占的大小相同。
64
1064
使用nametuple可以简单地定义一个类,第一个参数类名,第二个参数为类的属性名(以'type1 type2 type3'为字符串定义),赋值的操作可参考上面的例子。还可以通过.操作符访问属性值。但不可以修改属性值。
3.改进版的nametuple–typing.NameTuple
两个nametuple的功能类似,主要区别在于后者支持用新语法来定义记录类型并支持。
import sys
from typing import NamedTuple
class Car(NamedTuple):
color: str
mileage: float
automatic: bool
car1 = Car('red', 3812.4, True)
print(car1)
print(car1.color)
# car1.color = 'black' // AttributeError:不支持修改属性值。
print(sys.getsizeof(car1))
print(sys.getsizeof(('red', 3812.4, True)))
Car(color='red', mileage=3812.4, automatic=True) // 具有不错的__repr__
red // 通过字段访问属性值
64 //消耗较小的资源
64
typing.NameTuple支持简介风格的类型注释,风格比较清爽,可以用于定义简单的类(风格类似结构体),但要注意属性值不可修改。
4.struct.Struct–序列化C结构体
该类用于在Python和C结构体之间转换,并将其序列化为Python字节对象。例如可以用来处理存储在文件中或来自网络连接的二进制数据。
结构体使用与格式化字符串类似的语法来定义,能够定义并组织各种C数据类型。
from struct import Struct
Mystruct = Struct('i?f')
data = Mystruct.pack(23, False, 42.0)
print(data)
print(Mystruct.unpack(data))
b'\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00(B'
(23, False, 42.0)
5.type.SimpleNamespace–简单的命名空间,花里胡哨的属性访问
如果需要动态的向对象中添加、修改、删除属性,可尝试使用该类
from types import SimpleNamespace
user = SimpleNamespace(name='Jason', account='12345', password='123')
print(user.name)
print(user)
user.account = '54321'
user.isVIP = True
print(user)
Jason
namespace(account='12345', name='Jason', password='123')
namespace(account='54321', isVIP=True, name='Jason', password='123')
对了,’**'现在可以派上用场,用数据创建简单的对象。
data = {'name': 'Jason', 'account': '12345', 'password': '123'}
user = SimpleNamespace(**data)
print(user)
namespace(account='12345', name='Jason', password='123')
本文介绍了Python中几种重要的数据结构,包括`array.array`用于存储同类型数据,`collections.namedtuple`创建具有字段名的轻量级类,`typing.NamedTuple`提供更现代的类型注解支持,`struct.Struct`用于序列化C结构体,以及`type.SimpleNamespace`作为简单的动态属性容器。这些工具在处理特定数据格式和提高代码可读性方面非常有用。
4069

被折叠的 条评论
为什么被折叠?



