《(本科)第3章 序列ppt课件.pptx》由会员分享,可在线阅读,更多相关《(本科)第3章 序列ppt课件.pptx(99页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、课程主讲人:(本科)第3章 序列ppt课件Chap3 SequenceDepartment of Computer Science and TechnologyDepartment of University Basic Computer TeachingNanjing UniversityNanjing University序列3Nanjing University序列aStr = Hello, World!aList = 2, 3, 5, 7, 11aTuple = (Sunday, happy )x = range(10) pList = (AXP, American Express C
2、ompany, 78.51),(BA, The Boeing Company, 184.76),(CAT, Caterpillar Inc., 96.39),(CSCO, Cisco Systems, Inc., 33.71),(CVX, Chevron Corporation, 106.09)4序列是一种最基本最重要的数据结构Nanjing University5A AStrings字符串B BLists列表C CTuples元组D Drange objectsrange对象Nanjing University3.1.1 索引6Nanjing University序列的索引 序列类型对象一般
3、有多个成员组成,每个成员通常称为元素,每个元素都可以通过索引(index)进行访问,索引用方括号“”表示。如:7Nanjing University序列的索引8week0123456Monday Tuesday WednesdayThursdayFridaySaturday Sunday-7-6-5-4-3-2-1序列0-N1-(N-1)2-(N-2)N-2-2N-1-1访问模式 元素从0开始通过下标偏移量访问 一次可访问一个或多个元素Nanjing University索引的使用9 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aLis
4、t1Tues. aList-1Sun. aStr = apple aStr1pSourceNanjing University序列相关操作10值比较对象身份比较布尔运算切片重复连接判断成员序列类型转换内建函数序列类型其他内建函数标准类型运算通用序列类型操作序列类型函数Nanjing University3.1.2 标准类型运算11Nanjing University标准类型运算符=!=值比较isis not对象身份比较notandor布尔运算12Nanjing University值比较13 apple 1,3,5 != 2,4,6True aList1 = Tues.True 1, Mond
5、ay o, k (o, k)Traceback (most recent call last): File , line 1, in o, k (o, k)TypeError: unorderable types: list() 1 , 2 , 3 1 , a , 3Traceback (most recent call last): File , line 1, in 1 , 2 , 3 1 , a , 3TypeError: unorderable types: int() aTuple = (BA, The Boeing Company, 184.76) bTuple = aTuple
6、bTuple is aTupleTrue cTuple = (BA, The Boeing Company, 184.76) aTuple is cTupleFalse aTuple = cTupleTrueSourceNanjing University布尔(逻辑)运算15 ch = k a = ch = z or A = ch aStr = American Express Company aStr9: 16ExpressSourceNanjing University切片19 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aList
7、 0: 5Mon., Tues., Wed., Thur., Fri. aList: 5Mon., Tues., Wed., Thur., Fri. aList5: 7Sat., Sun.SourceNanjing University切片20 aList-2: -1Sat. aList-2: -3 aList-2:Sat., Sun. aList:Mon., Tues., Wed., Thur., Fri., Sat., Sun.SourceNanjing University切片21切片操作的另一种格式,可以选择切片操作时的步长:sequencestartindex : endindex
8、: stepsaList0: 5 aList0: 5: 10123456Nanjing University切片22 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aList1: 6: 3Tues., Fri. aList:3Mon., Thur., Sun. aList:-3Sun., Thur., Mon. aList5: 1: -2Sat., Thur.SourceNanjing University切片23 aStr = apple aStr0: 3app aTuple = (3, 2, 5, 1, 4, 6) aTuple1:
9、: 2(2, 1, 6)SourceNanjing University切片24 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. day = aListint(input(The day of the week(1-7): ) - 1The day of the week(1-7): 5 print( Today is + day + .)Today is Fri.SourceNanjing University切片25 week = Monday, Tuesday, Wednesday, Thursday, Friday, Saturda
10、y, Sunday print(week1, week-2, n, week1:4, n, week:6, n, week:-1)Tuesday SaturdayTuesday, Wednesday, Thursday Monday, Tuesday, Wednesday, Thursday, Friday, Saturday Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, MondaySourceNanjing University重复26重复操作的形式为:sequence * copies apple * 3appleappl
11、eapple (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 * 2P, y, t, h, o, n, P, y, t, h, o, nSourceNanjing University连接27连接操作的形式为:sequence1 + sequence2 1, 2, 3 + 4, 5, 61, 2, 3, 4, 5, 6 (1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6) pine
12、 + applepineapple t, h, e + appleTraceback (most recent call last): File , line 1, in t, h, e + appleTypeError: can only concatenate list (not str) to listSourceNanjing University判断成员28 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. Mon. in aListTrue week in aListFalse week not in aListTrueSourc
13、e判断一个元素是否属于一个序列操作的形式为:obj in sequenceobj not in sequenceNanjing University判断成员29 username = Jack, Tom, Halen, Rain input(please input your name: ) in usernameplease input your name: HalenTrueSourceNanjing University3.1.4 序列类型函数30Nanjing University序列类型转换内建函数31list()tuple() str() list(Hello, World!)H,
14、 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)1, 2, 3 tuple(1, 2, 3)(1, 2, 3)SourceNanjing University序列类型转换内建函数32list()tuple() str() str(123)123 str(t,h,e)(t, h, e)SourceNanjing University序列类型其他常用内建函数33len() enumerate()sorted() max()sum()min
15、() zip() reversed() aStr = Hello, World! len(aStr)13 sorted(aStr) , !, , H, W, d, e, l, l, l, o, o, rSourceNanjing University序列类型其他常用内建函数34 aStr = Hello, World! len(aStr)13Source nList = 3, 2, 5, 1 sorted(nList)1, 2, 3, 5 nList3, 2, 5, 1Sourcelen()sorted()Nanjing University序列类型其他常用内建函数35 nList = 3,
16、2, 5, 1 reversed(nList) list(reversed(nList)1, 5, 2, 3Sourcereversed() Nanjing University序列类型其他常用内建函数36 sum(a, b, c)Traceback (most recent call last): File , line 1, in sum(a, b, c)TypeError: unsupported operand type(s) for +: int and str sum(1, 2, 3.5)6.5Sourcesum()Nanjing University序列类型其他常用内建函数37
17、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, 3Sourcemax()和min()Nanjing University序列类型其他常用内建函数38 seasons = Spring, Summer, Fall, Winter list(enumerate(seasons)(0, Spring), (1, Summer), (2, Fall), (3, Winte
18、r) list(enumerate(seasons, start = 1)(1, Spring), (2, Summer), (3, Fall), (4, Winter)Sourceenumerate()Nanjing University序列类型其他常用内建函数39 list(zip(hello, world)(h, w), (e, o), (l, r), (l, l), (o, d)Sourcezip()Nanjing University字符串40Nanjing University3.2.1 字符串的表示41Nanjing University字符串的表示形式42 aStr = The
19、 Boeing Company bStr = The Boeing Company cStr = The Boeingcompany aStrThe Boeing Company bStrThe Boeing Company cStrThe BoeingnCompanySource单引号三引号双引号Nanjing University字符串的表示形式43 dStr = Im a student. dStrIm a student. eStr = No pain, No gain. is a good saying. eStrNo pain, No gains. is a good saying
20、. break fast # breakfast或breakfast等形式亦可breakfastSourceNanjing University字符串的表示形式44 cStr = The Boeingcompany cStrThe BoeingnCompany fStr = Its said that. where there is a will, there is a way. fStrIts said thatnwhere there is a will, there is a way.Source三引号分行输入Nanjing University字符串的表示形式45 gStr = rd:
21、pythonn.py gStrd:pythonn.pySource原始字符串操作符Nanjing University字符串的创建和访问46 aStr = The Boeing Company print(football)footballSource aStr = The Boeing Company hStr = aStr:4 + IBM + aStr-8: hStrThe IBM CompanySource创建方式:访问方式:直接输出赋值切片Nanjing University字符串的创建和访问不可变47 testStr = hello testStr0 = HTraceback (mo
22、st recent call last): File , line 1, in testStr0 = HTypeError: str object does not support item assignment hStrThe IBM Company hStr = hStrSourceNanjing University常用转义字符48字符说明t横向制表符n换行r回车双引号单引号反斜杠(在行尾时)续行符ooo 值为八进制数ooo的字符xhh值为十六进制数hh的字符 aStr = 101tx41n bStr = 141tx61n print(aStr, bStr)AAaaSourceNanji
23、ng University字符串常用方法49capitalize() center()count()encode()endswith() find()format()index()isalnum()isalpha()isdigit()islower()isspace()istitle()isupper()join()ljust()lower()lstrip()maketrans()partition() replace()rfind()rindex()rjust()rpartition()rstrip()split()splitlines() startswith()strip()swapca
24、se()title()translate() upper()zfill()Nanjing University字符串常用方法50 aStr = Python! aStr.center(11) Python! Sourcecenter() bStr = No pain, No gain. bStr.count(no)0 bStr.count(No)2Sourcecount()Nanjing University字符串小例子51给出一个字符串,不区分大小写,字符串中可能包含A-Z,a-z, (空格)等字符。输出字母a(包括大小写)出现的次数。测试数据:abc&ABC。# Filename: cha
25、r_count.pys1 = abc&ABCs = s1.lower()n = s.count(a)print(n)FileNanjing University字符串常用方法52 bStr = No pain, No gain. # 逗号后面有一个空格! bStr.find(No)0 bStr.find(no)-1 bStr.find(No, 3)9 bStr.find(No, 3, 10)-1 bStr.find(No, 3, 11)9Sourcefind()Nanjing University字符串常用方法53 bStr = No pain, No gain. # 逗号后面有一个空格! b
26、Str.index(no)Traceback (most recent call last): File , line 1, in bStr.index(no)ValueError: substring not found bStr.index(No, 3, 10)Traceback (most recent call last): File , line 1, in bStr.index(No, 3, 10)ValueError: substring not foundSourceindex()Nanjing University字符串常用方法54 love .join(I, Python!
27、) I love Python! .join(Hello, World)Hello, World -.join(BA, The Boeing Company, 184.76)BA-The Boeing Company-184.76 .join(2020,1,1)2020.1.1Sourcejoin()Nanjing University字符串常用方法55 cStr = Hope is a good thing. cStr.replace(Hope, Love)Love is a good thing.Sourcereplace()Nanjing University字符串常用方法56split
28、() 2020 1 1.split()2020, 1, 1 dStr = I am a student. dStr:-1.split()I, am, a, student 2020.1.1.split(.)2020, 1, 1SourceNanjing University字符串常用方法57split() nums = input(Enter the nums: ).split(,)Enter the nums: 12,34,56 nums12, 34, 56SourceNanjing University字符串的应用58有一些从网络上下载的类似如下形式的一些句子:What do you th
29、ink of this saying No pain, No gain?对于句子中双引号中的内容,首先判断其是否满足标题格式,不管满足与否最终都将其转换为标题格式输出。Nanjing University# Filename: totitle.pyaStr = What do you think of this saying No pain, No gain?lindex = aStr.index(,0,len(aStr)rindex = aStr.rindex(,0,len(aStr)tempStr = aStrlindex+1:rindexif tempStr.istitle(): pri
30、nt(It is title format.)else: print(It is not title format.)print(tempStr.title()File字符串的应用59tempStr= aStr.split()1Nanjing University字符串常用方法60strip() hellon.strip()helloSourceNanjing University 列表61Nanjing University列表62BAC经典的序列类型可变的容器包含不同类型元素Nanjing University3.3.1 列表的表示63Nanjing University列表的表示64 a
31、List = P , y, t, h, o, n pList = 1, BA, The Boeing Company, 184.76Source中括号Nanjing University列表的创建65 aList = pList = 1, BA, The Boeing Company, 184.76 cList = x for x in range(1, 10, 2) dList = list(Python) eList = 0 * 10Source空括号赋值List内建函数列表解析Nanjing University列表的创建66可扩展的容器对象包含不同类型对象 aList = list(H
32、ello.) aListH, e, l, l, o, . aList = list(hello.) aListh, e, l, l, o, . aList0 = H aListH, e, l, l, o, .Source bList = 1, 2, a, 3.5SourceNanjing University列表的创建 aList = 1, 2, 3, 4, 5 names = Zhao, Qian, Sun, Li bList = 3, 2, 1, Action pList = (AXP, American Express Company, 78.51), (BA, The Boeing C
33、ompany, 184.76),(CAT, Caterpillar Inc., 96.39),(CSCO, Cisco Systems, Inc., 33.71),(CVX, Chevron Corporation, 106.09)67Nanjing University列表的操作68 pList = (AXP, American Express Company, 78.51), (BA, The Boeing Company, 184.76), (CAT, Caterpillar Inc., 96.39), (CSCO, Cisco Systems, Inc., 33.71), (CVX,
34、Chevron Corporation, 106.09) pList11The Boeing Company pList:2(AXP, American Express Company, 78.51), (BA, The Boeing Company, 184.76)SourceNanjing University列表的操作69 fList = list(hello)h, e, l, l, o fList0 = H fListH, e, l, l, oSource可变的列表可以修改元素值Nanjing University列表的方法70append() copy()count()extend(
35、)index()insert()pop()remove()reverse()sort()参数的作用:list.sort(key=None, reverse=False) numList = 3, 11, 5, 8, 16, 1 fruitList = apple, banana, pear, lemon, avocado numList.sort(reverse = True) numList16, 11, 8, 5, 3, 1 fruitList.sort(key = len) fruitListpear, apple, lemon, banana, avocadoSourceNanjing
36、 University列表的方法71 aList = 1, 2, 3 aList.append(4) aList1, 2, 3, 4 aList.append(5, 6) aList1, 2, 3, 4, 5, 6 aList.append(Python!) aList1, 2, 3, 4, 5, 6, Python!Sourceappend()Nanjing University bList = 1, 2, 3 bList.extend(4) bList1, 2, 3, 4 bList.extend(5, 6) bList1, 2, 3, 4, 5, 6 bList.extend(Pytho
37、n!) bList1, 2, 3, 4, 5, 6, P, y, t, h, o, n, !Source列表的方法72extend()Nanjing University列表的方法73 bList = 1, 2, 3 bList.extend(4)Traceback (most recent call last): File , line 1, in bList.extend(4)TypeError: int object is not iterableSourceextend()Nanjing University a = 1, 2, 3, 4 b = a.copy() # b = a:也是
38、浅拷贝 b1, 2, 3, 4 b0, b20 = 5, 5 b5, 2, 5, 4 a1, 2, 5, 4Source列表的方法74copy()浅拷贝Nanjing University import copy a = 1, 2, 5, 4 c = copy.deepcopy(a) c1, 2, 5, 4 c0, c20 = 8, 8 c8, 2, 8, 4 a1, 2, 5, 4Source列表的方法75copy()深拷贝Nanjing University scores = 7, 8, 8, 8, 8.5, 9, 9, 9, 10, 10 scores.pop()10 scores7,
39、8, 8, 8, 8.5, 9, 9, 9, 10 scores.pop(4)8.5 scores7, 8, 8, 8, 9, 9, 9, 10Source列表的方法76pop()Nanjing University jScores = 7, 8, 8, 8, 9, 9, 9, 10 jScores.remove(9) jScores7, 8, 8, 8, 9, 9, 10Source列表的方法77remove()Nanjing University week = Mon., Tues., Wed., Thur., Fri., Sat., Sun. week.reverse() weekSun
40、., Sat., Fri., Thur., Wed., Tues., Mon.Source列表的方法78reverse()Nanjing University列表的方法79 序列类型的内建函数 返回的是序列逆序排序后的迭代器,原列表内容不变。reversed() 列表的方法 在原列表上直接翻转,并得到逆序列表,改变原列表内容。列表.reverse()字符串和元组(字符串和元组都是不可变的)没有reverse()方法Nanjing UniversitySource jScores = 9, 9, 8.5, 10, 7, 8, 8, 9, 8, 10 jScores.sort() jScores7
41、, 8, 8, 8, 8.5, 9, 9, 9, 10, 10 numList = 3, 11, 5, 8, 16, 1 fruitList = apple, banana, pear, lemon, avocado numList.sort(reverse = True) numList16, 11, 8, 5, 3, 1 fruitList.sort(key = len) fruitListpear, apple, lemon, banana, avocado列表的方法80sort()Nanjing University列表的方法81 序列类型的内建函数 返回的是排序后的新列表,原列表内容
42、不变。sorted() 列表的方法 对原列表排序,改变原列表内容。列表.sort()字符串和元组(字符串和元组都是不可变的)没有sort()方法Nanjing University列表的应用82某学校组织了一场校园歌手比赛,每个歌手的得分由10名评委和观众决定,最终得分的规则是去掉10名评委所打分数的一个最高分和一个最低分,再加上所有观众评委分数后的平均值。评委打出的10个分数为:9、9、8.5、10、7、8、8、9、8和10,观众评委打出的综合评分为9,请计算该歌手的最终得分。Nanjing University列表的应用837, 8, 8, 8, 8.5, 9, 9, 9, 10, 108
43、, 8, 8, 8.5, 9, 9, 9, 108, 8, 8, 8.5, 9, 9, 9, 10, 98.72222222222# Filename: scoring.pyjScores = 9, 9, 8.5, 10, 7, 8, 8, 9, 8, 10aScore = 9jScores.sort()jScores.pop()jScores.pop(0)jScores.append(aScore)aveScore = sum(jScores)/len(jScores)print(aveScore)FileNanjing University列表的应用84有一份参加Python课程的学号名单
44、B01,B02,B03,B05,B08,B10,请计算共有多少同学参与了本课程。请分别用列表和字符串的方法来解决这个问题。Nanjing University列表的应用85# Filename: count.pylst = B01,B02,B03,B05,B08,B10s = B01,B02,B03,B05,B08,B10num1 = len(lst)print(num1)num2 = s.count(,) + 1num3 = len(s.split(,)print(num2, num3)FileNanjing University元组86Nanjing University元组的创建87 a
45、Tuple = (1, 2, 3) aTuple(1, 2, 3) 2020,(2020,) k = 1, 2, 3 k(1, 2, 3)Source圆括号Nanjing University元组的操作88 bTuple = (Monday, 1, 2,3) bTuple(Monday, 1, 2, 3) bTuple011 len(bTuple)3 bTuple1:(2, 3)Source序列通用:切片、求长度Nanjing University元组的操作89Source aList = AXP, BA, CAT aTuple = (AXP, BA, CAT) aList1 = INTC p
46、rint(aList)AXP, INTC, CAT aTuple1= INTCTraceback (most recent call last): File , line 1, in aTuple1= INTCTypeError: tuple object does not support item assignment aTuple.sort()Traceback (most recent call last): File , line 1, in aTuple.sort()AttributeError: tuple object has no attribute sort元组不可变Nanj
47、ing University元组90 aTuple = (3, 5, 2, 4) sorted(aTuple)2, 3, 4, 5 aTuple(3, 5, 2, 4) aTuple.sort()Traceback (most recent call last): File , line 1, in AttributeError: tuple object has no attribute sortSource aList = 3, 5, 2, 4 aList3, 5, 2, 4 sorted(aList)2, 3, 4, 5 aList3, 5, 2, 4 aList.sort() aLis
48、t2, 3, 4, 5SourceNanjing University元组91 序 列 的 内 建函数 返 回 排 序 新列 表 , 原 列表内容不变sorted() 元组没有sort方法。sort()Nanjing University3.4.2 元组的其他特性和作用92Nanjing University元组特性93 bTuple = (1, 2, 3, 4) bTuple2 = 5, 6Traceback (most recent call last): File , line 1, in bTuple2 = 5, 6TypeError: tuple object does not su
49、pport item assignment bTuple20 = 5 bTuple(1, 2, 5, 4)Source元组的可变元素可变Nanjing University元组的作用元组用在什么地方? 在映射类型中当作键使用函数的特殊类型参数未明确定义的一组对象94Nanjing University函数返回一组值、未明确定义列表还是元组 def foo(): return 1, 2, 3 foo() (1, 2, 3) 1,2,3(1, 2, 3)Source返回对象的个数返回类型0None1object1tuple95Nanjing Universityrange对象96Nanjing U
50、niversityrange对象 用range()函数生成range对象,执行时一边计算一边产生值(类似一个生成器),生成一个不可变的整数序列97range(start, end, step=1)range(start, end)range(end)Nanjing Universityrange对象98 list(range(3, 11)3, 4, 5, 6, 7, 8, 9, 10 list(range(11)0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 list(range(3, 11, 2)3, 5, 7, 9Source list(range(0, -10, -1)