鍍金池/ 教程/ Python/ 回顧 list 和 str
標(biāo)準(zhǔn)庫 (4)
如何成為 Python 高手
標(biāo)準(zhǔn)庫 (6)
標(biāo)準(zhǔn)庫 (3)
類(2)
Pandas 使用 (2)
xml
用 tornado 做網(wǎng)站 (5)
文件(1)
練習(xí)
列表(3)
從小工到專家
除法
錯誤和異常 (2)
函數(shù)(1)
用 tornado 做網(wǎng)站 (7)
為做網(wǎng)站而準(zhǔn)備
函數(shù)練習(xí)
標(biāo)準(zhǔn)庫 (8)
Pandas 使用 (1)
回顧 list 和 str
字典(1)
用 tornado 做網(wǎng)站 (3)
字符串(1)
函數(shù)(2)
寫一個簡單的程序
將數(shù)據(jù)存入文件
語句(5)
SQLite 數(shù)據(jù)庫
集成開發(fā)環(huán)境(IDE)
集合(1)
類(1)
用 tornado 做網(wǎng)站 (6)
用 tornado 做網(wǎng)站 (2)
自省
語句(4)
錯誤和異常 (1)
用 tornado 做網(wǎng)站 (4)
集合(2)
列表(1)
標(biāo)準(zhǔn)庫 (1)
生成器
mysql 數(shù)據(jù)庫 (1)
第三方庫
實(shí)戰(zhàn)
運(yùn)算符
類(3)
字典(2)
語句(1)
數(shù)和四則運(yùn)算
語句(2)
文件(2)
MySQL 數(shù)據(jù)庫 (2)
電子表格
迭代器
mongodb 數(shù)據(jù)庫 (1)
特殊方法 (2)
特殊方法 (1)
字符編碼
編寫模塊
用 tornado 做網(wǎng)站 (1)
標(biāo)準(zhǔn)庫 (5)
函數(shù)(4)
類(5)
字符串(2)
關(guān)于 Python 的故事
函數(shù)(3)
字符串(4)
處理股票數(shù)據(jù)
常用數(shù)學(xué)函數(shù)和運(yùn)算優(yōu)先級
字符串(3)
為計算做準(zhǔn)備
多態(tài)和封裝
類(4)
迭代
語句(3)
錯誤和異常 (3)
分析 Hello
Python 安裝
標(biāo)準(zhǔn)庫 (2)
列表(2)
元組

回顧 list 和 str

list 和 str 兩種類型數(shù)據(jù),有不少相似的地方,也有很大的區(qū)別。本講對她們做個簡要比較,同時也是對前面有關(guān)兩者的知識復(fù)習(xí)一下,所謂“溫故而知新”。

相同點(diǎn)

都屬于序列類型的數(shù)據(jù)

所謂序列類型的數(shù)據(jù),就是說它的每一個元素都可以通過指定一個編號,行話叫做“偏移量”的方式得到,而要想一次得到多個元素,可以使用切片。偏移量從 0 開始,總元素數(shù)減 1 結(jié)束。

例如:

>>> welcome_str = "Welcome you"
>>> welcome_str[0]
'W'
>>> welcome_str[1]
'e'
>>> welcome_str[len(welcome_str)-1]
'u'
>>> welcome_str[:4]
'Welc'
>>> a = "python"
>>> a*3
'pythonpythonpython'

>>> git_list = ["qiwsir","github","io"]
>>> git_list[0]
'qiwsir'
>>> git_list[len(git_list)-1]
'io'
>>> git_list[0:2]
['qiwsir', 'github']
>>> b = ['qiwsir']
>>> b*7
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']

對于此類數(shù)據(jù),下面一些操作是類似的:

>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str   #用 + 號連接 str
'hello,world,Welcome you'
>>> welcome_str             #原來的 str 沒有受到影響,即上面的+號連接后重新生成了一個字符串
'Welcome you'
>>> first
'hello,world'

>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list     #用 + 號連接 list,得到一個新的 list
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['Python']

>>> len(welcome_str)    # 得到字符數(shù)
11
>>> len(git_list)       # 得到元素數(shù)
3

另外,前面的講述中已經(jīng)說明了關(guān)于序列的基本操作,此處不再重復(fù)。

區(qū)別

list 和 str 的最大區(qū)別是:list 是可以改變的,str 不可變。這個怎么理解呢?

首先看對 list 的這些操作,其特點(diǎn)是在原處將 list 進(jìn)行了修改:

>>> git_list
['qiwsir', 'github', 'io']

>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']

>>> git_list[1]               
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']

>>> git_list.insert(1,"algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']

>>> git_list.pop()
'python'

>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']

以上這些操作,如果用在 str 上,都會報錯,比如:

>>> welcome_str
'Welcome you'

>>> welcome_str[1]='E'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

>>> del welcome_str[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object doesn't support item deletion

>>> welcome_str.append("E")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

如果要修改一個 str,不得不這樣。

>>> welcome_str
'Welcome you'
>>> welcome_str[0]+"E"+welcome_str[2:]  #從新生成一個 str
'WElcome you'
>>> welcome_str                         #對原來的沒有任何影響
'Welcome you'

其實(shí),在這種做法中,相當(dāng)于重新生成了一個 str。

多維 list

這個也應(yīng)該算是兩者的區(qū)別了,雖然有點(diǎn)牽強(qiáng)。在 str 中,里面的每個元素只能是字符,在 list 中,元素可以是任何類型的數(shù)據(jù)。前面見的多是數(shù)字或者字符,其實(shí)還可以這樣:

>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix[0][1]
2
>>> mult = [[1,2,3],['a','b','c'],'d','e']
>>> mult
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
>>> mult[1][1]
'b'
>>> mult[2]
'd'

以上顯示了多維 list 以及訪問方式。在多維的情況下,里面的 list 被當(dāng)成一個元素對待。

list 和 str 轉(zhuǎn)化

以下涉及到的 split()join()在前面字符串部分已經(jīng)見過。一回生,二回熟,這次再見面,特別是在已經(jīng)學(xué)習(xí)了列表的基礎(chǔ)上,應(yīng)該有更深刻的理解。

str.split()

這個內(nèi)置函數(shù)實(shí)現(xiàn)的是將 str 轉(zhuǎn)化為 list。其中 str=""是分隔符。

在看例子之前,請看官在交互模式下做如下操作:

>>>help(str.split)

得到了對這個內(nèi)置函數(shù)的完整說明。特別強(qiáng)調(diào):這是一種非常好的學(xué)習(xí)方法

split(...) S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

不管是否看懂上面這段話,都可以看例子。還是希望看官能夠理解上面的內(nèi)容。

>>> line = "Hello.I am qiwsir.Welcome you." 

>>> line.split(".")     #以英文的句點(diǎn)為分隔符,得到 list
['Hello', 'I am qiwsir', 'Welcome you', '']

>>> line.split(".",1)   #這個 1,就是表達(dá)了上文中的:If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']       

>>> name = "Albert Ainstain"    #也有可能用空格來做為分隔符
>>> name.split(" ")
['Albert', 'Ainstain']

下面的例子,讓你更有點(diǎn)驚奇了。

>>> s = "I am, writing\npython\tbook on line"   #這個字符串中有空格,逗號,換行\(zhòng)n,tab 縮進(jìn)\t 符號
>>> print s         #輸出之后的樣式
I am, writing
python  book on line
>>> s.split()       #用 split(),但是括號中不輸入任何參數(shù)
['I', 'am,', 'writing', 'Python', 'book', 'on', 'line']

如果 split()不輸入任何參數(shù),顯示就是見到任何分割符號,就用其分割了。

"[sep]".join(list)

join 可以說是 split 的逆運(yùn)算,舉例:

>>> name
['Albert', 'Ainstain']
>>> "".join(name)       #將 list 中的元素連接起來,但是沒有連接符,表示一個一個緊鄰著
'AlbertAinstain'
>>> ".".join(name)      #以英文的句點(diǎn)做為連接分隔符
'Albert.Ainstain'
>>> " ".join(name)      #以空格做為連接的分隔符
'Albert Ainstain'

回到上面那個神奇的例子中,可以這么使用 join.

>>> s = "I am, writing\npython\tbook on line" 
>>> print s
I am, writing
python  book on line
>>> s.split()
['I', 'am,', 'writing', 'Python', 'book', 'on', 'line']
>>> " ".join(s.split())         #重新連接,不過有一點(diǎn)遺憾,am 后面逗號還是有的。怎么去掉?
'I am, writing python book on line'

總目錄   |   上節(jié):列表(3)   |   下節(jié):元組

如果你認(rèn)為有必要打賞我,請通過支付寶:qiwsir@126.com,不勝感激。

上一篇:實(shí)戰(zhàn)下一篇:文件(2)