>>welcome_str="Welcomeyou">>>welcome_str[0]'W'>>>welcome_str[1]'e'>>>welcome_str[len(welcome_str)-1]'u'>>>welcome_str[:4]'Welc'>>>" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

跟老齊學Python之list和str比較

系統 1917 0

相同點

都屬于序列類型的數據

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

例如:

            
>>> 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']


          

對于此類數據,下面一些操作是類似的:

            
>>> 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)  #得到字符數
11
>>> len(git_list)    #得到元素數
3


          

區別

list和str的最大區別是:list是原處可以改變的,str則原處不可變。這個怎么理解呢?

首先看對list的這些操作,其特點是在原處將list進行了修改:

            
>>> 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 "
            
              ", line 1, in 
              
                
TypeError: 'str' object does not support item assignment

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

>>> welcome_str.append("E")
Traceback (most recent call last):
File "
                    
                      ", line 1, in 
                      
                        
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'

          

其實,在這種做法中,相當于從新生成了一個str。

多維list

這個也應該算是兩者的區別了,雖然有點牽強。在str中,里面的每個元素只能是字符,在list中,元素可以是任何類型的數據。前面見的多是數字或者字符,其實還可以這樣:

            
>>> 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也跟一個前面元素一樣對待。

list和str轉化

str.split()

這個內置函數實現的是將str轉化為list。其中str=""是分隔符。

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

>>>help(str.split)
得到了對這個內置函數的完整說明。特別強調:這是一種非常好的學習方法

            
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.

          

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

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

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

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

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


          

join可以說是split的逆運算,舉例:

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

          


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久久青草线蕉亚洲麻豆 | 99re热线精品视频 | 成人一区二区免费中文字幕 | 国产精品不卡在线观看 | 亚洲欧美日韩中文字幕在线一 | 在线精品免费视频 | 久久www免费人成看片入口 | 中国大陆高清aⅴ毛片 | 欧美亚洲国产精品久久久久 | 依人综合网 | 中文字幕在线高清 | 亚洲人jizz| 七七七久久久久人综合 | 国产精品视频在线免费观看 | 久久成人视 | 中文字幕亚洲一区二区三区 | 亚洲日本在线播放 | 五月天色区 | 久久夜夜操妹子 | 国产中文一区 | 日本精品在线视频 | 久久网免费视频 | 欧美综合精品一区二区三区 | 久久久高清国产999尤物 | 婷婷激情综合网 | 日本一级大黄毛片免费基地 | 日本免费毛片在线高清看 | 超碰最新上传 | 亚洲在线视频观看 | 97久久精品国产精品青草 | 午夜影院免费 | 成人在线观看一区 | 成年午夜视频免费观看视频 | 中文字幕不卡免费高清视频 | 国产香蕉偷在线观看视频 | 日韩精品亚洲人成在线播放 | 亚洲国产欧美在线 | a毛片在线 | 日韩欧美视频 | 狠狠色噜噜狠狠狠狠奇米777 | 中文字幕亚洲精品第一区 |