python array_python数组array.array(转帖)
關于array:
Python 本身沒有數組這個說法, 有的就是list和tuple, list就具有其他語言中的數組特性.
至于list和tuple的區別,在于list可以在運行時修改內容和大小,tuple在首次創建和賦值后, 不可以再次修改內部的內容
不過python 有提供一個array模塊,用于提供基本數字,字符類型的數組.用于容納字符號,整型,浮點等基本類型.
這種模塊主要用于二進制上的緩沖區,流的操作
比如說修改MP3的ID3V1標簽,就可以用到array
以下是array支持的初始化類型
Type codeC TypePython TypeMinimum size in bytes
'c'
char
character
1
'b'
signed char
int
1
'B'
unsigned char
int
1
'u'
Py_UNICODE
Unicode character
2 (see note)
'h'
signed short
int
2
'H'
unsigned short
int
2
'i'
signed int
int
2
'I'
unsigned int
long
2
'l'
signed long
int
4
'L'
unsigned long
long
4
'f'
float
float
4
'd'
double
float
8
使用demo:
創建一個interger類型的數組
myarr = array(“l”) <——–創建數組
myarr.append(3) <——–追加元素
myarr.append(1)
myarr.append(8)
刪除最后一個
myarr.pop()
刪除第一個指定的X
myarr.remove(x)
取數組的值,通過下標
num1 = myarr[0] <———–第一個值
指定位置,插入值
myarr.insert(6,10)
6表示下標,10表示要插入的值
數組反序
myarr.reverse()
總結
以上是生活随笔為你收集整理的python array_python数组array.array(转帖)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: micropython实现多任务_pyt
- 下一篇: python range函数for_Py