Python中的数组

1 数组的创建:

涉及的module: array

 

An array object is similar to a list except that it can hold only certain types of simple data and only one type at any given time. when you create an array object,you specify which type of data it will hold:

 

>>> import array
>>> z=array.array('B')
>>> z.append(5)
>>> z
array('B', [5])
>>> z[0]
5
>>> q= array.array('i',[5,10,20])
>>> q
array('i', [5, 10, 20])
>>> q[0]
5

 

数组支持的数据类型:

 

求数组的长度:

arrayname.itemsize;

>>> q.itemsize
4

 

数组和list 之间转换:

数组转换为list:toList(): converts the array to an ordinary list

>>> q.tolist()
[5, 10, 20]

 

将一个list添加到数组的末尾:

>>> q
array('i', [5, 10, 20])
>>> q.fromlist([3,4])
>>> q
array('i', [5, 10, 20, 3, 4])

 

array 和string之间的相互转换:

tostring():onvert an array to a sequence of bytes using the tostring() methond.

fromstring():taking a string of bytes and converting them to values for the array

 

数组和文件之间的互操作:

tofile(file):convert the array to a sequence of bytes and writes the resulting bytes to a file you pass in

fromfiles(file,count): reads the specifiled number of items in from a file object and appends them to the array.

 

 

 

 

 

### Python数组的使用方法与数据结构 #### 基础概念 Python 的内置数据类型中并没有严格意义上的 “数组”。通常所说的数组实际上是通过 `list` 或者第三方库如 NumPy 提供的数据结构实现的。标准的 Python 列表是一个动态数组,能够存储任意类型的对象[^1]。 然而,在科学计算领域,NumPy 库提供了更为高效的多维数组对象 (`ndarray`) 和一系列操作函数。相比普通的列表,NumPy 数组具有更高的性能和更低的内存消耗,因此成为数据分析和机器学习的基础工具之一。 --- #### 创建数组的方式 以下是创建数组的一些常见方式: 1. **使用列表 (List)** Python 自带的列表是一种通用型容器,支持多种数据类型的混合存储。 ```python my_list = [1, 2, 3, 'a', 'b'] ``` 2. **使用 NumPy 数组** 如果需要高性能的数值运算,则推荐使用 NumPy 的 `array` 函数。 ```python import numpy as np # 从列表转换成 NumPy 数组 my_numpy_array = np.array([1, 2, 3]) # 创建全零数组 zeros_array = np.zeros((3, 3)) # 创建全一数组 ones_array = np.ones((2, 4)) # 创建随机数数组 random_array = np.random.rand(5, 5) ``` --- #### 数组的操作 以下是一些常见的数组操作及其代码示例: 1. **求和** 对于简单的列表,可以直接使用内置的 `sum()` 方法;对于 NumPy 数组,则有专门的 `.sum()` 方法。 ```python # 列表求和 lst_sum = sum([1, 2, 3]) # 结果为 6 # NumPy 数组求和 array_sum = np.sum(np.array([1, 2, 3])) # 结果同样为 6 ``` 2. **去重** 可以利用集合或列表推导式完成数组的去重操作。 ```python # 使用 set 进行去重 arr = [1, 2, 2, 3, 4, 4, 5] unique_set = list(set(arr)) # 结果为 [1, 2, 3, 4, 5] # 使用列表推导式保持顺序 unique_lst = [] [unique_lst.append(x) for x in arr if x not in unique_lst] print(unique_lst) # 输出 [1, 2, 3, 4, 5] ``` 3. **追加数据** 在列表中可以通过 `.append()` 方法添加单个元素,或者通过 `.extend()` 添加多个元素。而在 NumPy 数组中则需借助 `np.concatenate()` 或其他函数。 ```python # 列表追加 my_list = [1, 2, 3] my_list.append(4) # 结果为 [1, 2, 3, 4] # NumPy 数组追加 my_np_array = np.array([1, 2, 3]) new_array = np.append(my_np_array, [4]) # 结果为 [1, 2, 3, 4] ``` --- #### 性能对比 虽然 Python 列表功能强大且灵活,但在处理大规模数值数据时效率较低。相比之下,NumPy 数组由于其底层优化设计(基于 C 编写),在速度和空间占用方面表现优异。 例如,当涉及矩阵乘法或其他复杂数学运算时,直接调用 NumPy 提供的功能会显著优于手动编写循环逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值