Python程序设计-第2章-使用序列-1(第3次课).ppt

上传人:豆**** 文档编号:24784628 上传时间:2022-07-07 格式:PPT 页数:52 大小:728KB
返回 下载 相关 举报
Python程序设计-第2章-使用序列-1(第3次课).ppt_第1页
第1页 / 共52页
Python程序设计-第2章-使用序列-1(第3次课).ppt_第2页
第2页 / 共52页
点击查看更多>>
资源描述

《Python程序设计-第2章-使用序列-1(第3次课).ppt》由会员分享,可在线阅读,更多相关《Python程序设计-第2章-使用序列-1(第3次课).ppt(52页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、计算机编程导论计算机编程导论 西南林业大学西南林业大学计算机与信息学院计算机与信息学院2013.9ReviewChapter 1 sequential programming Use computers to solve problems Essentials of program design Flow chart Program running Problem-solving process Sequential programming复习第第1章章 顺序程序设计顺序程序设计 用计算机解决问题的方法用计算机解决问题的方法 程序设计要素程序设计要素 流程图流程图 程序运行过程程序运行过程 解

2、决问题的过程解决问题的过程 顺序程序设计顺序程序设计Chapter 2 Using Array Array are frequently used in storing data in program design. Almost every kind of programming language provides a array data structure, such as C and Basic provide one-dimensional, multi-dimensional array. Python provides the richest, the most flexible

3、, and the most powerful forms in program design. This chapter describes the array structures (lists, tuples, dictionaries) in Python, and how to use them to achieve simple and powerful programs.第第2章章 使用序列使用序列 序列是程序设计中经常用到的数据存储方式,序列是程序设计中经常用到的数据存储方式,几乎每一种程序设计语言都提供了表格数据结几乎每一种程序设计语言都提供了表格数据结构,如构,如C和和Ba

4、sic中的一维、多维数组等。中的一维、多维数组等。 Python提供的序列类型在所有程序设计语言提供的序列类型在所有程序设计语言中是最丰富,最灵活,也是功能最强大的。中是最丰富,最灵活,也是功能最强大的。 本章介绍使用本章介绍使用Python中常用的序列结构(列中常用的序列结构(列表、元组、字典)来实现一些简单而又功能强表、元组、字典)来实现一些简单而又功能强大的程序。大的程序。 2.1 Array ProblemQ2-1 Data sorting Description: Read 5 data from the keyboard, output in ascending order. So

5、lutions 1: Use five variables to store data, compare and sort.2.1 序列问题序列问题【问题问题2-1】 数据排序问题数据排序问题 问题描述:由用户从键盘输入五个数据,按升问题描述:由用户从键盘输入五个数据,按升序排序后输出。序排序后输出。 解决方案一:用五个变量来存储输入的五个数解决方案一:用五个变量来存储输入的五个数据,再用一一比较和交换来达到排序的目的。据,再用一一比较和交换来达到排序的目的。2.1 序列问题序列问题问题问题2-1程序:程序:#data sort Ques2_1_0.pyx1 = input(请输入第请输入第1

6、个元素个元素:)x2 = input(请输入第请输入第2个元素个元素:)x3 = input(请输入第请输入第3个元素个元素:)x4 = input(请输入第请输入第4个元素个元素:)x5 = input(请输入第请输入第5个元素个元素:)#将第将第1个元素排好序个元素排好序if x1x2: x1,x2=x2,x1if x1x3: x1,x3=x3,x1if x1x4: x1,x4=x4,x1if x1x5: x1,x5=x5,x1#将第将第2个元素排好个元素排好 if x2x3: x2,x3=x3,x2if x2x4: x2,x4=x4,x2if x2x5: x2,x5=x5,x2#将将第第

7、3个元素排好个元素排好 if x3x4: x3,x4=x4,x3if x3x5: x3,x5=x5,x3#将将第第4个元素排好,则第个元素排好,则第5个也排好个也排好 if x4x5: x4,x5=x5,x4#输出排序结果输出排序结果print x1,x2,x3,x4,x52.1 Array ProblemShortcoming of Solution 1: Need to define many variables to store data The program is not complex, but tedious, error-prone When sorting more data

8、, such as 50, the programming becomes intolerable When sorting even more data, such as 1000, it will be ridiculous to write a program in this way.2.1 序列问题序列问题解决方案一存在的问题:解决方案一存在的问题: 需要定义多个变量来存储数据需要定义多个变量来存储数据 程序虽不复杂,但很繁琐,写起来容易出错程序虽不复杂,但很繁琐,写起来容易出错 当待排序数据达到一定量,如当待排序数据达到一定量,如50个时,这样的个时,这样的编程方式变得无法忍受编程方

9、式变得无法忍受 当待排序数据达到一定量,如当待排序数据达到一定量,如1000个时,这个时,这样的程序无法编,无法看样的程序无法编,无法看2.1 Array ProblemSolution 2:Use ListAnalysis: Input ten data, and store them in a list Use pythons sort () method to sort the list data and output.结束结束输出排序后的结果输出排序后的结果开始开始用用sort( )方法对列表中数据进行排序方法对列表中数据进行排序输入输入10个数据个数据存放在列表中存放在列表中图图2-

10、1 2-1 数据排序流程图数据排序流程图2.1 序列问题序列问题解决方案二:解决方案二:使用列表使用列表分析:分析: 将用户输入的十个数将用户输入的十个数据存放到一个列表中据存放到一个列表中 用用python的的sort( )方方法对列表中数据进行法对列表中数据进行排序后输出。排序后输出。 结束结束输出排序后的结果输出排序后的结果开始开始用用sort( )方法对列表中数据进行排序方法对列表中数据进行排序输入输入10个数据个数据存放在列表中存放在列表中图图2-1 2-1 数据排序流程图数据排序流程图2.1 序列问题序列问题问题问题2-2程序:程序: #data sort:Ques2_1.py d

11、ata_list= #初始化一个空列表初始化一个空列表#循环十次,输入十个数字放到列表中循环十次,输入十个数字放到列表中for integer in range(10): x=input(请输入第请输入第+str(integer+1)+个元素:个元素:) data_list=data_list+xprint 排序前数据:排序前数据:,data_list#用用sort方法对列表中的数据进行排序方法对列表中的数据进行排序data_list.sort()print 排序后数据:排序后数据:,data_list方案二优点:方案二优点:不管数据量多大,只用定义一不管数据量多大,只用定义一个列表变量个列表

12、变量程序简单,程序代码量不随数程序简单,程序代码量不随数据量变大而增加据量变大而增加程序可以调用程序可以调用Python内置函数内置函数来实现排序来实现排序2.1 序列问题序列问题输入及程序运行结果:输入及程序运行结果:请输入第请输入第1个元素:个元素:54请输入第请输入第2个元素:个元素:23请输入第请输入第3个元素:个元素:67请输入第请输入第4个元素:个元素:84请输入第请输入第5个元素:个元素:41请输入第请输入第6个元素:个元素:68请输入第请输入第7个元素:个元素:34请输入第请输入第8个元素:个元素:56请输入第请输入第9个元素:个元素:98请输入第请输入第10个元素:个元素:6

13、1排序前数据:排序前数据: 54, 23, 67, 84, 41, 68, 34, 56, 98, 61排序后数据:排序后数据: 23, 34, 41, 54, 56, 61, 67, 68, 84, 982.1 Array ProblemQ2-3 Dictionary problem Description: According to the keyword (abbreviations) from users input, find name corresponding explanation Analysis: some words are stored in the dictionar

14、y, the key is the first letter of the word, the value is the explanation of the word. The user enters a valid letter, the program queries the corresponding word and output it. If the input is out of range, the program ends.2.1 序列问题序列问题【问题问题2-3】 查字典问题查字典问题 问题描述:根据用户输入的关键字的简写查询问题描述:根据用户输入的关键字的简写查询相应名称

15、解释。相应名称解释。 分分 析:将一些程序设计中常用名称存放析:将一些程序设计中常用名称存放在字典中,键是其英文的第一个字母,值是该在字典中,键是其英文的第一个字母,值是该名称的解释。由用户输入要查询的名称的英语名称的解释。由用户输入要查询的名称的英语第一个字母,若在合法的范围内则进行查询、第一个字母,若在合法的范围内则进行查询、输出,若不在范围内则结束程序。输出,若不在范围内则结束程序。2.1 序列问题序列问题算法流程图:算法流程图:开始开始定义字典定义字典结束结束图图2-2 2-2 查字典流程图查字典流程图输入输入ae范围内的字母范围内的字母输出查字典的结果输出查字典的结果2.1 序列问题

16、序列问题问题问题2-3程序:程序:#Dictionary Search:Ques2_3.py #定义字典定义字典dic=a:algorithm,算法,解决一种问题的大致步骤,算法,解决一种问题的大致步骤,b:bug,臭虫,程序里的错误,臭虫,程序里的错误,c:compile,编译,编译,把用高级程序语言写的程序转换成低级语言把用高级程序语言写的程序转换成低级语言,d:debugging,除虫,找到及移除程序设计错误的过,除虫,找到及移除程序设计错误的过程程,e:exception,异常,执行错误的另一个名称,异常,执行错误的另一个名称#输入要查询的关键字以便进行字典查询输入要查询的关键字以便进

17、行字典查询keyword=raw_input(请输入要查询的名词关键字(请输入要查询的名词关键字(ae):)#输入输入ae之间的关键字则进行查询,否则结束程序之间的关键字则进行查询,否则结束程序while keyword=a and keyword2.2 Array BasicsArray: A series of values, which are usually related to, and in a certain order.Array c:Include 12 integer elementsUse Array:Array nameposition numberThe positi

18、on of the first element is 0, c0The second element is c1The i-th element is ci-1Array can also be accessed from the rear:The last element is c-1Penultimate 2 is c-2Position number also known as the subscript or Index2.2 序列基础知识序列基础知识序列:序列:一系列连续值,它们通常是相关的,并且按一定一系列连续值,它们通常是相关的,并且按一定顺序排列。顺序排列。序列序列 c:12

19、个整数元素个整数元素引用元素:序列名引用元素:序列名位置编号位置编号第第1个元素的位置编号为个元素的位置编号为 0,c0第第 2个元素是个元素是 c1第第 i个元素是个元素是ci-1 序列也可以从尾部访问序列也可以从尾部访问:最后一个元素是最后一个元素是 c-1倒数第倒数第2个是个是 c-2倒数第倒数第 i个元素是个元素是 c-i位置编号位置编号也称也称“下标下标”或或“索引索引” ,是整数或整数表达式。,是整数或整数表达式。2.3 ListList: A list is an ordered set of values, where each value is identified by a

20、n index. The elements of a list can be any type Each data in the list called an element All its elements are separated by commas and placed in the bracket and List of examples: 10, 20, 30, 40#All the elements are integer data crunchy frog, ram bladder, lark vomit #All the elements are strings spam,

21、2.0, 5, 10, 20#The list contains a string element, a floating-point type element, an integer elements and a list2.3 列表列表列表:列表: 是是Python中内置数据类型,是一个元素的有序集合中内置数据类型,是一个元素的有序集合 一个列表中的数据类型可以各不相同一个列表中的数据类型可以各不相同 列表中的每一个数据称为元素列表中的每一个数据称为元素 其所有元素用逗号分割并放在一对中括号其所有元素用逗号分割并放在一对中括号“”和和“”中中列表举例:列表举例: 10, 20, 30, 4

22、0#所有元素都是整型数据的列表所有元素都是整型数据的列表 crunchy frog, ram bladder, lark vomit#所有所有元素都是字符串的列表元素都是字符串的列表 spam, 2.0, 5, 10, 20#该列表中包含该列表中包含了一个字符串元素、一个浮点类型元素、一个整型元素了一个字符串元素、一个浮点类型元素、一个整型元素和一个列表类型元素和一个列表类型元素2.3 List(1) Create: use the = to assign a list to a variable.For example: a_list = a, b, mpilgrim, z, example

23、(2) Read elements We can access an element in the list by using the variable name and an index number. Pay attention to the list of the first elements of a serial number 0.For example: print a_list2mpilgrimNote: If a list has n elements, then the valid range is from -n to n-1, when the index number

24、x is negative, indicating that the counting starts from the right, and the actual index number of the element is n + x .2.3 列表列表(1)创建列表:使用)创建列表:使用“=”将一个列表赋值给变量。将一个列表赋值给变量。例如:例如: a_list = a, b, mpilgrim, z, example(2)读取元素)读取元素 用变量名加元素序号(放中括号中)即可访问列表中某用变量名加元素序号(放中括号中)即可访问列表中某个元素,注意列表的第一个元素序号为个元素,注意列表的

25、第一个元素序号为0。例如:例如: print a_list2mpilgrim注意:若一个列表有注意:若一个列表有n个元素,则访问元素的合法序号范个元素,则访问元素的合法序号范围是围是-nn-1,当序号,当序号x为负时,表示从右边计数,其访为负时,表示从右边计数,其访问的元素实际为序号为问的元素实际为序号为n+x的元素。的元素。2.3 列表列表例如:例如: print(a_list-1)example print(a_list-5)a print(a_list-7)Traceback (most recent call last): File , line 1, in print(a_list-

26、7)IndexError: list index out of range print(a_list5)Traceback (most recent call last): File , line 1, in print(a_list5)IndexError: list index out of rangea_list = a, b, mpilgrim, z, example2.3 List(3) Slices Use a range of indexes to intercept any part of the list to get a new list. The first number

27、 represents the slice start position, and the second number represents the slice cutoff (but does not contain) position.For example: print(a_list1:3)b, mpilgrim print(a_list1:-1)b, mpilgrim, za_list = a, b, mpilgrim, z, example2.3 列表列表(3)列表切片)列表切片 可以使用可以使用“列表序号对列表序号对”来截取列表中的任何部分,来截取列表中的任何部分,从而得到一个新列

28、表。序号对中第一个序号表示切片从而得到一个新列表。序号对中第一个序号表示切片开始位置,第二个序号表示切片截止(但不包含)位开始位置,第二个序号表示切片截止(但不包含)位置。置。 例如:例如: print(a_list1:3)b, mpilgrim print(a_list1:-1)b, mpilgrim, z2.3 列表列表注意:当切片的左索引为注意:当切片的左索引为0时可时可缺省缺省,当右索引,当右索引为列表长度时也可为列表长度时也可缺省缺省。例如:例如: print(a_list:3)a, b, mpilgrim print(a_list3:)z, example print(a_list

29、:)a, b, mpilgrim, z, examplea_list = a, b, mpilgrim, z, example2.3 List(4) Adding elementsMethod 1: Use the “+” to attach a new list in the tail of a original list a_list = 1 a_list = a_list + a, 2.0 a_list1, a, 2.0 Method 2: Uses append () method to add a new element to the end of a list; a_list.ap

30、pend(True) a_list1, a, 2.0, True 2.3 列表列表(4)增加元素)增加元素方法一:方法一:使用使用“+”将一个新列表附加在原列表的尾部;将一个新列表附加在原列表的尾部; a_list = 1 a_list = a_list + a, 2.0 a_list1, a, 2.0方法二:方法二:使用使用append( )方法向列表尾部添加一个新元素;方法向列表尾部添加一个新元素; a_list.append(True) a_list1, a, 2.0, True2.3 ListMethod 3: Use the extend () method to add a lis

31、t at the tail of the original list; a_list.extend(x, 4) a_list1, a, 2.0, True, x, 4 Method 4: Use the insert () method to add an element into the list anywhere. a_list.insert(0, x) a_listx, 1, a, 2.0, True, x, 42.3 列表列表方法三:使用方法三:使用extend( )方法将一个列表添加在原列表的方法将一个列表添加在原列表的尾部;尾部; a_list.extend(x, 4) a_lis

32、t1, a, 2.0, True, x, 4方法四:使用方法四:使用insert( )方法将一个元素插入到列表的任方法将一个元素插入到列表的任意位置。意位置。 a_list.insert(0, x) a_listx, 1, a, 2.0, True, x, 42.3 List(5) Search Use count ( ) method to calculate the number of an element in the list; a_list.count(x)2 Use ”in” Operator checking whether an element is in the list;

33、3 in a_listFalse 2.0 in a_listTruea_list = x, 1, a, 2.0, True, x, 42.3 列表列表(5)检索元素)检索元素使用使用count( )方法计算列表中某个元素出现的次数;方法计算列表中某个元素出现的次数; a_list.count(x)2使用使用in运算符返回某个元素是否在该列表中;运算符返回某个元素是否在该列表中; 3 in a_listFalse 2.0 in a_listTrue2.3 列表列表 Use the index () method to return the exact location of an element

34、 in the list; 使用使用index()方法返回某个元素在列表中的准确位置;方法返回某个元素在列表中的准确位置; a_list.index(x)0 a_list.index(5)Traceback (most recent call last): File , line 1, in a_list.index(5)ValueError: 5 is not in lista_list = x, 1, a, 2.0, True, x, 42.3 List(6) Delete an element When adding or removing elements, the list will

35、 automatically expand or shrink. The list will never have gaps.Method 1: Use the “del” statement to delete the elements at a particular location del a_list1 a_listx, a, 2.0, True, x, 4a_list = x, 1, a, 2.0, True, x, 42.3 列表列表(6)删除元素)删除元素 当向列表中添加或删除元素时,列表将自动拓展或收缩,列表中永远不会有缝隙。方法一:方法一:使用del语句删除某个特定位置的元素

36、 del a_list1 a_listx, a, 2.0, True, x, 42.3 列表列表Method2: Use “remove()” method to remove an element with a particular value方法二:使用方法二:使用remove方法删除方法删除某个特定值的元素某个特定值的元素 a_list.remove(x) a_lista, 2.0, True, x, 4 a_list.remove(x) a_lista, 2.0, True, 4 a_list.remove(x)Traceback (most recent call last): Fi

37、le , line 1, in a_list.remove(x)ValueError: list.remove(x): x not in lista_list = x, a, 2.0, True, x, 42.3 列表列表Method three: Use pop() method to pop out the element at the specified location. The last element is popped out if no parameters given.方法三:使用方法三:使用pop(参数参数)方法来弹出(删除)指定位置的元素,缺省参方法来弹出(删除)指定位置

38、的元素,缺省参数时弹出最后一个元素。数时弹出最后一个元素。 a_list.pop( )4 a_lista, 2.0, True a_list.pop(1)2.0 a_lista, True a_list.pop(1)True a_lista a_list.pop( )a a_list a_list.pop( )Traceback (most recent call last): File , line 1, in a_list.pop( )IndexError: pop from empty list2.3.3 Function of the list1. cmp( )Format:cmp(L

39、ist1,List2)Function:Compare the two lists,returns 1 when the first list is greater than the second, on the contrary returns -1, when the two elements of the list is the same returns 02. len( )Format:len(List)Function:Returns the number of elements in the list2.3.3 列表常用函数列表常用函数1. cmp( )格式:格式:cmp(列表列表

40、1,列表,列表2)功能:功能:对两个列表进行比较,若第一个列表大于对两个列表进行比较,若第一个列表大于第二个,则结果为第二个,则结果为1,相反则为,相反则为-1,元素完全,元素完全相同则结果为相同则结果为0。 2. len( )格式:格式:len(列表列表)功能:功能:返回列表中的元素个数。返回列表中的元素个数。 2.3.3 Function of the list list1=123,xyz list2=123,abc cmp(list1,list2)1 list2=123,z cmp(list1,list2)-1 list2=list1 cmp(list1,list2)0 len(list

41、1)2 2.3.3 Function of the list3. max( ),min( )Format:max(List), min(List)Function:Return the largest or smallest element in the list str_l=abc,xyz,123 num_l=123,456,222 max(str_l)xyz min(str_l)123 max(num_l)456 min(num_l)1232.3.3 列表常用函数列表常用函数3. max( )和和min( ) 格式:格式:max(列表列表), min(列表列表)功能:功能:返回列表中返回列

42、表中的最大或最小元的最大或最小元素。素。 str_l=abc,xyz,123 num_l=123,456,222 max(str_l)xyz min(str_l)123 max(num_l)456 min(num_l)1232.3.3 Function of the list4. sorted( ) reversed( )Format:sorted(List), reversed(List)Function:Sorting or Reverse list=1,4,3,6,9,0,2 for x in reversed(list): print x,2 0 9 6 3 4 1 sorted(li

43、st)0, 1, 2, 3, 4, 6, 9 sorted(list,reverse=True)9, 6, 4, 3, 2, 1, 02.3.3 列表常用函数列表常用函数4. sorted( )和和reversed( ) 格式:格式:sorted(列表列表), reversed(列表列表)功能:功能:前者的功能是对前者的功能是对列表进行排序,默列表进行排序,默认是按升序排序,认是按升序排序,还可在列表的后面还可在列表的后面增加一个增加一个reverse参数,其等于参数,其等于True则表示按降序排序则表示按降序排序;后者的功能是对;后者的功能是对列表进行逆序。列表进行逆序。 list=1,4,

44、3,6,9,0,2 for x in reversed(list): print x,2 0 9 6 3 4 1 sorted(list)0, 1, 2, 3, 4, 6, 9 sorted(list,reverse=True)9, 6, 4, 3, 2, 1, 02.3.3 Function of the list5. sum( )Format:sum(List),Function:Summing the numerical list of elements sum(list)25 sum(str_l)Traceback (most recent call last): File , lin

45、e 1, in sum(str_l)TypeError: unsupported operand type(s) for +: int and str2.3.3 列表常用函数列表常用函数5. sum( ) 格式:格式:sum(列表列表)功能:功能:对数值型列表的元素进行求和运算,对非数值型对数值型列表的元素进行求和运算,对非数值型列表运算则出错。列表运算则出错。 sum(list)25 sum(str_l)Traceback (most recent call last): File , line 1, in sum(str_l)TypeError: unsupported operand type(s) for +: int and str上机作业上机作业1. 将本章课件中涉及的所有源程序在将本章课件中涉及的所有源程序在Python下输入、调试并运行;上交。下输入、调试并运行;上交。 (不需要书面版不需要书面版)2. 将本章课件中所有的交互式命令在将本章课件中所有的交互式命令在Python的的IDLE中调试;存盘、上交。中调试;存盘、上交。(不需要书面版不需要书面版)3. 习题习题2.14. 习题习题2.2

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com