W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
list和str兩種類型數(shù)據(jù),有不少相似的地方,也有很大的區(qū)別。本講對她們做個簡要比較,同時也是對前面有關(guān)兩者的知識復(fù)習(xí)一下,所謂“溫故而知新”。
所謂序列類型的數(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ù)。
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。
這個也應(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)成一個元素對待。
以下涉及到的split()
和join()
在前面字符串部分已經(jīng)見過。一回生,二回熟,這次再見面,特別是在已經(jīng)學(xué)習(xí)了列表的基礎(chǔ)上,應(yīng)該有更深刻的理解。
這個內(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ù),顯示就是見到任何分割符號,就用其分割了。
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'
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: