3序列字符串列表_第1頁
3序列字符串列表_第2頁
3序列字符串列表_第3頁
3序列字符串列表_第4頁
3序列字符串列表_第5頁
已閱讀5頁,還剩47頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第3章序列Chap3SequenceDepartmentofComputerScienceandTechnologyDepartmentofUniversityBasicComputerTeachingNanjingUniversity序列概述23.1序列分類3字符串列表集合元組

字典有序列表無序列表不可變序列可變序列序列中的元素,可以通過for循環(huán)語句進(jìn)行遍歷,稱之為迭代。能夠進(jìn)行迭代的對象,稱為可迭代對象序列的索引序列類型對象一般有多個成員組成,每個成員通常稱為元素,有序序列每個元素都可以通過索引(index)進(jìn)行訪問(字典用鍵訪問),索引用方括號“[]”表示。如:4sequence[index]序列的索引5week0123456'Monday''Tuesday''Wednesday''Thursday''Friday''Saturday''Sunday'-7-6-5-4-3-2-1訪問模式元素從0開始通過下標(biāo)偏移量訪問一次可訪問一個或多個元素索引的使用6>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>aList[1]'Tues.'>>>aList[-1]'Sun.'>>>aStr='apple'>>>aStr[1]'p'值比較7>>>'apple'<'banana'True>>>[1,3,5]!=[2,4,6]True>>>aList[1]=='Tues.'True>>>[1,'Monday']<[1,'Tuesday']True>>>['o','k']<('o','k')Traceback(mostrecentcalllast):File"<pyshell#0>",line1,in<module>['o','k']<('o','k')TypeError:unorderabletypes:list()<tuple()>>>[1,[2,3]]<[1,['a',3]]Traceback(mostrecentcalllast):File"<pyshell#1>",line1,in<module>[1,[2,3]]<[1,['a',3]]TypeError:unorderabletypes:int()<str()對象身份比較8>>>aTuple=('BA','TheBoeingCompany','184.76')>>>bTuple=aTuple>>>bTupleisaTupleTrue>>>cTuple=('BA','TheBoeingCompany','184.76')>>>aTupleiscTupleFalse>>>aTuple==cTupleTrue切片90123456切片操作的形式為:sequence[startindex:endindex]索引值切片10>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>aList[0:5]['Mon.','Tues.','Wed.','Thur.','Fri.']>>>aList[:5]['Mon.','Tues.','Wed.','Thur.','Fri.']>>>aList[5:7]['Sat.','Sun.']切片11注意:向前走步長需為正,向后走步長需為負(fù),否則切片為空。>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>aList[-2:-1]['Sat.']>>>aList[-2:-3][]>>>aList[-2:]['Sat.','Sun.']>>>aList[:]['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']切片12切片操作的另一種格式,可以選擇切片操作時的步長:sequence[startindex:endindex:steps]0123456切片13>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>aList[1:6:3]['Tues.','Fri.']>>>aList[::3]['Mon.','Thur.','Sun.']>>>aList[::-3]['Sun.','Thur.','Mon.']>>>aList[5:1:-2]['Sat.','Thur.']重復(fù)14重復(fù)操作的形式為:sequence*copies>>>‘a(chǎn)pple’

*3

#3*‘a(chǎn)pple’也一樣'appleappleapple'>>>(1,2,3)*2(1,2,3,1,2,3)>>>aTuple=(3,2,5,1)>>>aTuple*3(3,2,5,1,3,2,5,1,3,2,5,1)>>>['P'

,'y','t','h','o','n']*2['P','y','t','h','o','n','P','y','t','h','o','n']或者:copies*sequence連接15連接操作的形式為:sequence1+

sequence2>>>[1,2,3]+[4,5,6][1,2,3,4,5,6]>>>(1,2,3)+(4,5,6)(1,2,3,4,5,6)>>>'pine'+'apple''pineapple'>>>['t','h','e']+'apple'Traceback(mostrecentcalllast):File"<pyshell#2>",line1,in<module>['t','h','e']+'apple'TypeError:canonlyconcatenatelist(not"str")tolist判斷成員16>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>'Mon.'

in

aListTrue>>>'week'

inaListFalse>>>'week'

notinaListTrue判斷一個元素是否屬于一個序列操作的形式為:objinsequenceobjnotinsequence序列類型函數(shù)17序列類型轉(zhuǎn)換內(nèi)建函數(shù)18list()tuple()>>>list('Hello,World!')['H','e','l','l','o',',','','W','o','r','l','d','!']>>>tuple("Hello,World!")('H','e','l','l','o',',','','W','o','r','l','d','!')>>>list((1,2,3))#必須為兩層括號,意思是元祖轉(zhuǎn)換成列表[1,2,3]>>>tuple([1,2,3])(1,2,3)序列類型其他常用內(nèi)建函數(shù)19enumerate()len()reversed()sorted()max()sum()min()zip()>>>aStr='Hello,World!'>>>len(aStr)13>>>sorted(aStr)['','!',',','H','W','d','e','l','l','l','o','o','r']序列類型其他常用內(nèi)建函數(shù)20>>>nList=[3,2,5,1]>>>sorted(nList)[1,2,3,5]>>>nList[3,2,5,1]序列類型其他常用內(nèi)建函數(shù)21>>>nList=[3,2,5,1]>>>reversed(nList)<list_reverseiteratorobjectat0x0000018024361B70>>>>list(reversed(nList))[1,5,2,3]>>>nList[3,2,5,1]reversed()序列類型其他常用內(nèi)建函數(shù)22>>>sum(['a','b','c'])Traceback(mostrecentcalllast):File"<pyshell#3>",line1,in<module>sum(['a','b','c'])TypeError:unsupportedoperandtype(s)for+:'int'and'str'>>>sum([1,2,3.5])6.5sum()序列類型其他常用內(nèi)建函數(shù)23>>>aList=['Mon.','Tues.','Wed.','Thur.','Fri.','Sat.','Sun.']>>>max(aList)'Wed.'>>>max([1,2.5,3])3>>>max([1,5,3],[1,2.5,3])[1,5,3]>>>max([1,5,3,1],[1,9,3])[1,9,3]max()和min()字符串243.2字符串的表示形式25>>>aStr='TheBoeingCompany'>>>bStr="TheBoeingCompany">>>cStr='''TheBoeingcompany'''>>>aStr'TheBoeingCompany'>>>bStr'TheBoeingCompany'>>>cStr'TheBoeing\nCompany'單引號三引號雙引號字符串的表示形式26單引號里可以有雙引號,雙引號里可以有單引號>>>dStr="I'mastudent.">>>dStr"I'mastudent.">>>eStr='"Nopain,Nogain."isagoodsaying.'>>>eStr'"Nopain,Nogains."isagoodsaying.'字符串的創(chuàng)建和訪問——不可變27>>>hStr=‘’#單引號構(gòu)成的空串>>>hStr''>>>testStr='hello'>>>testStr[0]='H'Traceback(mostrecentcalllast):File"<pyshell#4>",line1,in<module>testStr[0]='H'TypeError:'str'objectdoesnotsupportitemassignment常用轉(zhuǎn)義字符28字符說明\t橫向制表符\n換行\(zhòng)r回車\"雙引號\'單引號\\反斜杠\(在行尾時)續(xù)行符\OOO八進(jìn)制數(shù)OOO代表的字符\xXX十六進(jìn)制數(shù)XX代表的字符>>>aStr='\101\t\x41\n'>>>bStr='\141\t\x61\n'>>>print(aStr,bStr)A Aa a字符串常用方法29>>>aStr='Python!'>>>aStr.center(11)'Python!'center()>>>bStr='Nopain,Nogain.'>>>bStr.count('no')0>>>bStr.count('No')2count()字符串常用方法30>>>bStr='Nopain,Nogain.'#逗號后面有一個空格!>>>bStr.find('No')0>>>bStr.find('no')-1>>>bStr.find('No',

3)9>>>bStr.find('No',3,10)-1>>>bStr.find('No',3,11)9find()字符串常用方法31>>>cStr='Hopeisagoodthing.'>>>cStr.replace("Hope",

'Love')'Loveisagoodthing.'replace()字符串常用方法32>>>'love'.join(['I','Python!'])'IlovePython!'>>>''.join(['Hello','World'])'HelloWorld'>>>'->'.join(('BA','TheBoeingCompany','184.76'))'BA->TheBoeingCompany->184.76'join()字符串常用方法33split()>>>'202011'.split()['2020','1','1']>>>dStr='Iamastudent'>>>dStr[:-1].split()['I','am','a','studen']>>>'2020.1.1'.split('.')['2020','1','1']列表343.3列表35BAC經(jīng)典的序列類型可變的容器包含不同類型元素列表的創(chuàng)建36>>>aList=[]>>>pList=[1,'BA','TheBoeingCompany',184.76]>>>cList=[xforxin

range(1,10,2)]#cList=[1,3,5,7,9]>>>dList=list('Python')空括號賦值List內(nèi)建函數(shù)列表解析中括號列表的創(chuàng)建37可擴(kuò)展的容器對象包含不同類型對象>>>aList=list('hello.')>>>aList['h','e','l','l','o','.']>>>aList[0]='H'>>>aList['H','e','l','l','o','.']>>>bList=[1,2,'a',3.5]列表的創(chuàng)建aList=[1,2,3,4,5]names=['Zhao','Qian','Sun','Li']bList=[3,2,1,'Action']pList=[('AXP','AmericanExpressCompany','78.51'),('BA','TheBoeingCompany','184.76'),('CAT','CaterpillarInc.','96.39'),('CSCO','CiscoSystems,Inc.','33.71'),('CVX','ChevronCorporation','106.09')]38列表的操作39>>>pList=[('AXP','AmericanExpressCompany','78.51'),

('BA','TheBoeingCompany','184.76'),

('CAT','CaterpillarInc.','96.39'),

('CSCO','CiscoSystems,Inc.','33.71'),

('CVX','ChevronCorporation','106.09')]>>>pList[1]('BA','TheBoeingCompany','184.76')>>>pList[1][1]'TheBoeingCompany'列表的方法40>>>aList=[1,2,3]>>>aList.append(4)>>>aList[1,2,3,4]>>>aList.append([5,6])>>>aList[1,2,3,4,[5,6]]>>>aList.append('Python!')>>>aList[1,2,3,4,[5,6],'Python!']append()向列表添加元素>>>bList=[1,2,3]>>>bList.extend([4])>>>bList[1,2,3,4]>>>bList.extend([5,6])>>>bList[1,2,3,4,5,6]>>>bList.extend('Python!')>>>bList[1,2,3,4,5,6,'P','y','t','h','o','n','!']列表的方法41extend()用序列來擴(kuò)展列表列表的方法42>>>bList=[1,2,3]>>>bList.extend(4)Traceback(mostrecentcalllast):File"<pyshell#7>",line1,in<module>bList.extend(4)TypeError:'int'objectisnotiterableextend()>>>a=[1,2,[3,4]]>>>b=a.copy()

#b=a[:]也是淺拷貝>>>b[1,2,[3,4]]>>>b[0],b[2][0]=5,5>>>b[5,2,[5,4]]>>>a[1,2,[5,4]]列表的方法43>>>b[2][0]isa[2][0]True>>>b[0]isa[0]Falsecopy()淺拷貝>>>import

copy#導(dǎo)入標(biāo)準(zhǔn)庫>>>a=[1,2,[5,4]]>>>c=copy.deepcopy(a)>>>c[1,2,[5,4]]>>>c[0],c[2][0]=8,8>>>c[8,2,[8,4]]>>>a[1,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論