網(wǎng)站開發(fā)者購(gòu)物支付模板競(jìng)價(jià)廣告是什么意思
這里寫自定義目錄標(biāo)題
- 一、基本認(rèn)識(shí)
- 二、list與傳統(tǒng)數(shù)組(以C++為例)的聯(lián)系與區(qū)別
- 三、1維list切片規(guī)則
- 四、2維list類似于2維數(shù)組,但表達(dá)方式需適應(yīng)
- 五、list與元組的聯(lián)系與區(qū)別
- 1. tuple的創(chuàng)建方法類似于list,tuple用()表示
- 2. tuple增加元素的方法
一、基本認(rèn)識(shí)
-
Python本身沒(méi)用傳統(tǒng)意義上的數(shù)組,但暫可把list看作數(shù)組(數(shù)組每個(gè)元素類型可以是任意的,不需要是同一類型),但是,也犧牲了數(shù)組的高效性
-
需要利用傳統(tǒng)數(shù)組的高效性,可在Python中import numpy的array
接下來(lái)從操作層面先認(rèn)識(shí)一下list、tuple與傳統(tǒng)數(shù)組的聯(lián)系與區(qū)別
二、list與傳統(tǒng)數(shù)組(以C++為例)的聯(lián)系與區(qū)別
C++:
數(shù)組聲明:typeName arrayName[arraySize]
如:float loans[20]
如果取元素,最基礎(chǔ)的方法就是,按下標(biāo)來(lái)操作,如:
float loans1=loan[0] #提取first alement
認(rèn)識(shí):與list容易混淆的是都用[ ],也是聯(lián)系之處,但是大部分語(yǔ)言基礎(chǔ)的數(shù)組都不支持動(dòng)態(tài)增加元素,C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,可借助一些技術(shù)方法。
Python的list:
Python中列表(list)是一種有序、可變且可重復(fù)元素集合。
在list中的數(shù)據(jù)類型保存的是數(shù)據(jù)的存放的地址,簡(jiǎn)單的說(shuō)就是指針,并非數(shù)據(jù),這樣保存一個(gè)list就太麻煩了,例如list1=[1,2,3,‘a(chǎn)’]需要4個(gè)指針和四個(gè)數(shù)據(jù)(引自:https://www.php.cn/faq/424062.html)
(其它可參考:https://blog.csdn.net/www_djh/article/details/134645540?spm=1001.2014.3001.5502)
犧牲了效率,增加了靈活性:
以下是關(guān)于Python列表的常見(jiàn)基本操作示例
list示例:
>>> stus=['x','=',2,y] #不需聲明,直接賦值
Traceback (most recent call last):File "<pyshell#0>", line 1, in <module>stus=['x','=',2,y]
NameError: name 'y' is not defined
>>> y=1
>>> stus=['x','=',2,y]
>>>
通過(guò)下標(biāo)(index)取元素:
>>> y=1
>>> stus=['x','=',2,y]
>>> stus[0]
'x'
>>> stus[-1]
1
>>>
查看某元素值在list的個(gè)數(shù)
>>> stus.count(2)#因?yàn)橹苯虞斎朐乇旧?#xff0c;當(dāng)本身是數(shù)值時(shí),有時(shí)很難理解
1 #返回2這個(gè)元素出現(xiàn)了一次
>>>
查看某元素的index,若有多個(gè)僅返回第一個(gè)index
>>> stus.index(2) #元素2的index
3
>>>
在末端增加一個(gè)元素
>>> stus.append(10) #append每次只增加一個(gè)元素
>>> stus
['x', '=', 2, 1, 10]
>>>
>>> stus.append([11,12,13])
>>> stus
['x', '=', 2, 1, 10, [11, 12, 13]]
>>>
也可添加元組、集合等
>>> stus.append((11,12,13))
>>> stus
['x', '=', 2, 1, 10, [11, 12, 13], (11, 12, 13)]
>>> stus.append({11,12,13})
>>> stus
['x', '=', 2, 1, 10, [11, 12, 13], (11, 12, 13), {11, 12, 13}]
>>>
也可在指定下標(biāo)處插入新值:
>>> stus.insert(0,3)
>>> stus
[3, 'x', '=', 2, 1, 10, [11, 12, 13], (11, 12, 13), {11, 12, 13}]
>>>
如果待插入的位置:正下標(biāo)超出范圍,則插在末端;負(fù)下標(biāo)超出范圍,則在最開始添加
刪除最后一個(gè)元素:stus.pop()并返回被刪除的元素
>>> stus.pop()
{11, 12, 13}
>>>
>>> stus.pop(1) #輸入待刪元素的index
'x'
>>>
用remove(元素),無(wú)返回
>>> stus
[3, '=', 2, 1, 10, [11, 12, 13], (11, 12, 13)]
>>> stus.remove(=)
SyntaxError: invalid syntax
>>> stus.remove("=")
>>> stus
[3, 2, 1, 10, [11, 12, 13], (11, 12, 13)]
>>>
用python 關(guān)鍵詞(命令)del 刪除某index的元素
>>> stus
[3, 2, 1, 10, [11, 12, 13], (11, 12, 13)]
>>> del stus[6]
Traceback (most recent call last):File "<pyshell#26>", line 1, in <module>del stus[6]
IndexError: list assignment index out of range
>>> del stus[5]
>>> stus
[3, 2, 1, 10, [11, 12, 13]]
>>>
其它:
>>> stus.sort() #該list元素類型不支持排序
Traceback (most recent call last):File "<pyshell#30>", line 1, in <module>stus.sort()
TypeError: '<' not supported between instances of 'list' and 'int'
>>>
倒序
>>> stus.reverse()
>>> stus
[[11, 12, 13], 10, 3, 2, 1]
>>>
清空
stus.clear()
三、1維list切片規(guī)則
如:>>> A
[‘a(chǎn)’, ‘b’, ‘a(chǎn)’, 60, 20]
則:A[::] 格式是 start_index:end_index:step
切片結(jié)果:包含 start_index元素,不包含end_index元素
因?yàn)閟tep缺省為1,往往被省略所以常見(jiàn)A[:]
如: A[:] 取全list
A[:2] 從index=0取到index<2的元素
A[2:] 從index=2取到末尾
這個(gè)東東初學(xué)容易與字典混在一起:字典是包括在一對(duì){ }內(nèi)的
如:dict = {‘Alice’: ‘2341’, ‘Beth’: ‘9102’, ‘Cecil’: ‘3258’}
>>> dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
>>> dict['Alice'] #也容易與在dataframe中的使用搞混,好難啊
'2341'
>>>
四、2維list類似于2維數(shù)組,但表達(dá)方式需適應(yīng)
C++:
二維數(shù)組聲明:typeName arrayName[rowSize][columnSize]
如:float loans[2][2]
而二維的list:
stuts_sheet=[[‘a(chǎn)’,‘b’],[60,20]]
取元素:
>>> stuts_sheet[0][0]
'a'
>>>
取行:
>>> stuts_sheet[0]
['a', 'b']
>>>
好像只能用代碼實(shí)現(xiàn)取列,而且,list每行元素可以不一樣長(zhǎng)
如:
>>> stuts_sheet[0].append('a')
>>> stuts_sheet
[['a', 'b', 'a'], [60, 20]]
>>>
extend可以把兩個(gè)list合并
>>> A=stuts_sheet[0]
>>> B=stuts_sheet[1]
>>> A.extend(b)
Traceback (most recent call last):File "<pyshell#42>", line 1, in <module>A.extend(b)
NameError: name 'b' is not defined
>>> A.extend(B)
>>> A
['a', 'b', 'a', 60, 20]
>>>
五、list與元組的聯(lián)系與區(qū)別
tuple一旦創(chuàng)建,它就不能改變了,也就是說(shuō)它也沒(méi)有 append(),insert() 這樣的方法,但它也有獲取某個(gè)索引值的方法,但是不能賦值。那么為什么要有 tuple 呢?那是因?yàn)?tuple 是不可變的,所以代碼更安全。函數(shù)經(jīng)常返回tuple。
1. tuple的創(chuàng)建方法類似于list,tuple用()表示
>>> tup=('aly',x)
>>> tup[1]
2
>>> x=[3]
>>> tup[1] #沒(méi)修改成功
2
>>>
2. tuple增加元素的方法
list可以用自己的append()來(lái)添加元素。
Python元組是一種不可變的有序集合。
方法一:
在Python中,元組和字符串一樣,支持“+”、“+=”運(yùn)算符進(jìn)行拼接操作。因此,可以通過(guò)將一個(gè)元組與一個(gè)元素的元組相加.
>>> t=(1,)
>>> t1=(a,b)
Traceback (most recent call last):File "<pyshell#3>", line 1, in <module>t1=(a,b)
NameError: name 'a' is not defined
>>> t1=('a','b')
>>> t2=t1+t
>>> t2
('a', 'b', 1)
>>>
方法二:
先轉(zhuǎn)成list利用list的append增加元素,再轉(zhuǎn)回tuple
>>> t2
('a', 'b', 1)
>>> list1=list(t2)
>>> list1.append(5)
>>> t3=tuple(list1)
>>> t3
('a', 'b', 1, 5)
>>>
方法三:
使用元組解包,這個(gè)方法的特點(diǎn):可以把元組元素設(shè)成某個(gè)變量,添加進(jìn)元組
>>> t3
('a', 'b', 1, 5)
>>> var=10
>>> t4=(*t3,var)
>>> t4
('a', 'b', 1, 5, 10)
>>>