跟老齊學(xué)Python之list和str比較
相同點(diǎn)
都屬于序列類型的數(shù)據(jù)
所謂序列類型的數(shù)據(jù),就是說(shuō)它的每一個(gè)元素都可以通過(guò)指定一個(gè)編號(hào),行話叫做“偏移量”的方式得到,而要想一次得到多個(gè)元素,可以使用切片。偏移量從0開始,總元素?cái)?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']
對(duì)于此類數(shù)據(jù),下面一些操作是類似的:
>>> first = "hello,world" >>> welcome_str 'Welcome you' >>> first+","+welcome_str #用+號(hào)連接str 'hello,world,Welcome you' >>> welcome_str #原來(lái)的str沒(méi)有受到影響,即上面的+號(hào)連接后從新生成了一個(gè)字符串 'Welcome you' >>> first 'hello,world' >>> language = ['python'] >>> git_list ['qiwsir', 'github', 'io'] >>> language + git_list #用+號(hào)連接list,得到一個(gè)新的list ['python', 'qiwsir', 'github', 'io'] >>> git_list ['qiwsir', 'github', 'io'] >>> language ['python'] >>> len(welcome_str) #得到字符數(shù) 11 >>> len(git_list) #得到元素?cái)?shù) 3
區(qū)別
list和str的最大區(qū)別是:list是原處可以改變的,str則原處不可變。這個(gè)怎么理解呢?
首先看對(duì)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上,都會(huì)報(bào)錯(cuò),比如:
>>> 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'
如果要修改一個(gè)str,不得不這樣。
>>> welcome_str 'Welcome you' >>> welcome_str[0] + "E" + welcome_str[2:] #從新生成一個(gè)str 'WElcome you' >>> welcome_str #對(duì)原來(lái)的沒(méi)有任何影響 'Welcome you'
其實(shí),在這種做法中,相當(dāng)于從新生成了一個(gè)str。
多維list
這個(gè)也應(yīng)該算是兩者的區(qū)別了,雖然有點(diǎn)牽強(qiáng)。在str中,里面的每個(gè)元素只能是字符,在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以及訪問(wèn)方式。在多維的情況下,里面的list也跟一個(gè)前面元素一樣對(duì)待。
list和str轉(zhuǎn)化
str.split()
這個(gè)內(nèi)置函數(shù)實(shí)現(xiàn)的是將str轉(zhuǎn)化為list。其中str=""是分隔符。
在看例子之前,請(qǐng)看官在交互模式下做如下操作:
>>>help(str.split)
得到了對(duì)這個(gè)內(nèi)置函數(shù)的完整說(shuō)明。特別強(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) #這個(gè)1,就是表達(dá)了上文中的:If maxsplit is given, at most maxsplit splits are done. ['Hello', 'I am qiwsir.Welcome you.'] >>> name = "Albert Ainstain" #也有可能用空格來(lái)做為分隔符 >>> name.split(" ") ['Albert', 'Ainstain'] "[sep]".join(list)
join可以說(shuō)是split的逆運(yùn)算,舉例:
>>> name ['Albert', 'Ainstain'] >>> "".join(name) #將list中的元素連接起來(lái),但是沒(méi)有連接符,表示一個(gè)一個(gè)緊鄰著 'AlbertAinstain' >>> ".".join(name) #以英文的句點(diǎn)做為連接分隔符 'Albert.Ainstain' >>> " ".join(name) #以空格做為連接的分隔符 'Albert Ainstain'
相關(guān)文章
Python的Flask框架中的Jinja2模板引擎學(xué)習(xí)教程
這篇文章主要介紹了Python的Flask框架中的Jinja2模板引擎學(xué)習(xí)教程,Jinja2模板引擎的用法也是Flask的Web開發(fā)中的重要知識(shí),需要的朋友可以參考下2016-06-06python使用celery實(shí)現(xiàn)異步任務(wù)執(zhí)行的例子
今天小編就為大家分享一篇python使用celery實(shí)現(xiàn)異步任務(wù)執(zhí)行的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python入門及進(jìn)階筆記 Python 內(nèi)置函數(shù)小結(jié)
這篇文章主要介紹了Python的內(nèi)置函數(shù)小結(jié),需要的朋友可以參考下2014-08-08淺談Python中threading join和setDaemon用法及區(qū)別說(shuō)明
這篇文章主要介紹了淺談Python中threading join和setDaemon用法及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05基于python實(shí)現(xiàn)操作redis及消息隊(duì)列
這篇文章主要介紹了基于python操作redis及消息隊(duì)列,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08vue學(xué)習(xí)筆記之動(dòng)態(tài)組件和v-once指令簡(jiǎn)單示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之動(dòng)態(tài)組件和v-once指令,結(jié)合簡(jiǎn)單實(shí)例形式詳細(xì)分析了vue.js動(dòng)態(tài)組建點(diǎn)擊切換相關(guān)操作技巧,需要的朋友可以參考下2020-02-02