xiuos/Ubiquitous/RT_Thread/micropython/docs/std-librarys/uarray.md

57 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## **array** 数字数据数组
**array** 模块定义了一个对象类型,它可以简洁地表示基本值的数组:字符、整数、浮点数。支持代码格式: b, B, h, H, i, I, l, L, q, Q, f, d (最后2个需要支持浮点数)。
### 构造函数
#### **class array.array**(typecode[, iterable])
用给定类型的元素创建数组。数组的初始内容由 iterable 提供,如果没有提供,则创建一个空数组。
```
typecode数组的类型
iterable数组初始内容
```
示例:
```python
>>> import array
>>> a = array.array('i', [2, 4, 1, 5])
>>> b = array.array('f')
>>> print(a)
array('i', [2, 4, 1, 5])
>>> print(b)
array('f')
```
### 方法
#### **array.append**(val)
将一个新元素追加到数组的末尾。
示例:
```python
>>> a = array.array('f', [3, 6])
>>> print(a)
array('f', [3.0, 6.0])
>>> a.append(7.0)
>>> print(a)
array('f', [3.0, 6.0, 7.0])
```
#### **array.extend**(iterable)
将一个新的数组追加到数组的末尾,注意追加的数组和原来数组的数据类型要保持一致。
示例:
```python
>>> a = array.array('i', [1, 2, 3])
>>> b = array.array('i', [4, 5])
>>> a.extend(b)
>>> print(a)
array('i', [1, 2, 3, 4, 5])
```
更多内容可参考 [array](http://docs.micropython.org/en/latest/pyboard/library/array.html) 。