{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 实践9 元组和列表\n", "\n", "1.熟悉列表的表示和操作\n", "2.掌握列表的遍历访问算法\n", "3.掌握批量数据的输入输出\n", "4.掌握批量数据的统计分析算法\n", "5.掌握查找和排序算法的实现\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. 元组和列表的基本操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (1) 元组的创建和访问" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "输入:t1='001001','LiSi','men',18,再输入:t1 显示该变量值,\n", "输入:t1[0]和t1[1]显示部分数据,最后输入:type(t1)显示变量类型,\n", "输入:len(t1)显示长度。\n", "\n", "t1[0]: __________\n", "t1[1]: __________\n", "t1的类型是:__________\n", "t1的长度是:__________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1='001001','LiSi','men',18\n", "t1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(t1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(t1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (2)列表的创建和访问" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "输入:t2=[’001001’,’Li Si’,’men’,18] ,\n", "再输入:t2显示该变量值,输入:t2[0]和t2[1]显示部分数据,\n", "最后输入:type(t2)显示变量类型,\n", "输入:men’in t2 测试成员。\n", "\n", "t2[0]:__________ \n", "t1[1]:__________ \n", "t2 的类型是:__________\n", "'men'in t2 的结果是:__________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2=[’001001’,’Li Si’,’men’,18] \n", "t2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(t2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'men’in t2 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (3)可变对象和不可变对象" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "输入:t2[3]+=1,\n", "再输入:t2,查看该变量值。\n", "输入:t1[3]+=1,显示出错信息。\n", "\n", "__________ 是可变对象。\n", "__________ 是不可变对象。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2[3]+=1\n", "t2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1[3]+=1\n", "t1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (4) 序列的基本操作" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "输入:t2+=['021-65789293'],\n", "再输入:t2,查看该变量值。\n", "输入:t2[0:1]=[],\n", "再输入:t2,查看该变量值。\n", "\n", "列表的“+”运算是:__________\n", "t2[0:1]=[]的作用是:__________\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2+=['021-65789293']\n", "t2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2[0:1]=[]\n", "t2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (5)列表元素的修改" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t2=['001001','Li Si','men',19,'021-65789293']\n", "t2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3=t2\n", "t3[3]=20\n", "t2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "说明:直接使用赋值运算得到的t3,与t2是指向同一个列表对象的,对t2和t3的操作实质是使用不同的名称对一个对象操作。" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "要得到一个对象副本,可使用列表的方法copy,复制后,两个列表的内容是相等的,但不是一个对象。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3=t2.copy()\n", "t3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3==t2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3 is t2" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "思考:“==”运算和“is”运算的区别__________________ " ] }, { "cell_type": "raw", "metadata": {}, "source": [ "也可以从一个空列表开始,将t2的内容加入到空列表中。设置一个空列表对象是必须的,因为之前t3并没有指定一个确定的数据类型。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3=[]\n", "t3.extend(t2)\n", "t3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3.append(1.78)\n", "t3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "思考:append与extend的区别________________ \n" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "insert方法支持在指定位置增加列表成员,例如在年龄的后面插入身高信息。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t3.insert(4,1.78)\n", "t3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#试一试:在列表的最前面插入整数序号15,得到列表:[15,'001001', 'Li Si', 'men', 19, 1.78, '021-65789293', 1.78]\n", "\n" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "remove方法可用于删除第一个指定值,pop可以删除并返回指定位置列表成员" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#删除列表中年龄后面多余的身高1.78\n", "#试一试,remove方法" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#试一试,pop方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 小试身手1" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "(1)\t已知列表 L1 和L2,由 L1 和 L2 构造L3,并回答问题。\n", " >>> L1=[1,2,3,4,5]\n", " >>> L2=[\"one\",\"two\",\"three\",\"four\",\"five\"]\n", " >>> L3=[[L1[1],L2[1]],[L1[2],L2[2]],[L1[3],L2[3]]]\n", "L3 的值是__________\n", "L3[1,1]的值是__________\n", "执行 L4=L3.pop(2)后,列表L3的值是__________,L4的值是__________。\n", "再执行L3.extend(L4),列表L3的值是__________。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L1=[1,2,3,4,5]\n", "L2=[\"one\",\"two\",\"three\",\"four\",\"five\"]\n", "L3=[[L1[1],L2[1]],[L1[2],L2[2]],[L1[3],L2[3]]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "raw", "metadata": {}, "source": [ "(2)假设执行了如下语句\n", ">>> s1=[0,1,2,3,4,5,6]\n", ">>> s2=['SUN','MON','TUE','WED','THU','FRI','SAT']\n", "利用 s1、s2 和列表操作,创建下列结果的序列对象。\n", "\n", "____________________\n", "\n", "____________________\n", "s5: [(0,'SUN'),(1,'MON'),(2,'TUE'),(3,'WED'),(4,'THU'),(5,'FRI'),(6,'SAT')]\n", "____________________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1=[0,1,2,3,4,5,6]\n", "s2=['SUN','MON','TUE','WED','THU','FRI','SAT']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#s3: 'SUN|MON|TUE|WED|THU|FRI|SAT'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#s4:[3,4,3,4,3,4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#s5: [(0,'SUN'),(1,'MON'),(2,'TUE'),(3,'WED'),(4,'THU'),(5,'FRI'),(6,'SAT')]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. 列表遍历的算法模式 \n", "(1)迭代访问\n", "```python\n", "\n", "for x in 迭代序列:\n", " … x … \n", "``` \n", "\n", "(2)下标访问\n", "```python\n", "\n", "L[M]:\n", "for i in range( M ):\n", " … L[i]…\n", "L[M][N]:\n", "for i in range( M ):\n", " for j in range( N ):\n", " … L[i][j]…\n", "\n", "`` \n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "L=[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2] [3, 4] [5, 6] [7, 8] [9, 10] " ] } ], "source": [ "for x in L:\n", " print(x,end=\" \")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6 7 8 9 10 " ] } ], "source": [ "for x in L:\n", " for y in x:\n", " print(y,end=\" \")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 \n", "3 4 \n", "5 6 \n", "7 8 \n", "9 10 \n" ] } ], "source": [ "for x in L:\n", " for y in x:\n", " print(y,end=\" \")\n", " print()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2] [3, 4] [5, 6] [7, 8] [9, 10] " ] } ], "source": [ "for i in range(5):\n", " print(L[i],end=\" \")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6 7 8 9 10 " ] } ], "source": [ "for i in range(5):\n", " for j in range(2):\n", " print(L[i][j],end=\" \")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6 7 8 9 10 " ] } ], "source": [ "for i in range(len(L)):\n", " for j in range(len(L[i])):\n", " print(L[i][j],end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 思考 下标访问和迭代访问适用场合\n", "试用不同的方法实现下面的功能" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#(1)将L中每一个元素扩大10倍" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#(2)输出列表L中每一个子列表的第2个元素" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#(3) 输出不规则列表Lx的每一个元素\n", "Lx=[[1],[2,3],[4],[5,6,7],[8,9],[10]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. 批量数据的创建" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1)逐个添加到列表 \n", "```python\n", "a=[]\n", "a.append(x)\n", "```\n", "\n", "例:输入批量数据到列表,统计它们的个数,总和以及平均值。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=[]\n", "x=input('input a number ,over quit:')\n", "while x!=\"over\":\n", " a.append(float(x)) #a+=[float(x)]\n", " x=input('input a number ,over quit:')\n", "n=len(a)\n", "if n==0:\n", " print(\"没有输入\")\n", "else:\n", " s=sum(a)\n", " print('n=',n,'sum=',s,'average=',s/n)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2)使用split函数同样可以处理以约定符号分隔一批数据的输入。进行字符串分裂处理后将数据存储在列表中。\n", "```python\n", "L=input().split(\",\")\n", "\n", "```\n", "例 输入若干以空格分隔的浮点数,保存到列表并输出。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入一组数据,以空格分隔:2.5 78 -12.4 44 8.21 -9.27\n", "[2.5, 78.0, -12.4, 44.0, 8.21, -9.27]\n" ] } ], "source": [ "L=input(\"请输入一组数据,以空格分隔:\").split() \n", "for i in range(len(L)):\n", " L[i]=float(L[i])\n", "print(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python语言还提供了更为简洁的方法来处理列表中数据类型的转换。 \n", "方法一 使用快速产生列表方式 \n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入一组数据,以空格分隔:2.5 78 -12.4 44 8.21 -9.27\n", "[2.5, 78.0, -12.4, 44.0, 8.21, -9.27]\n" ] } ], "source": [ "L=[float(x) for x in input(\"请输入一组数据,以空格分隔:\").split() ]\n", "print(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法二 map函数 \n", "map() 会根据提供的函数对指定序列做映射。函数格式如下: \n", "map(function, iterable, ...) \n", "第一个参数 function,第二个参数是序列,可以又多个序列。对参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。 \n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入一组数据,以空格分隔:2.5 78 -12.4 44 8.21 -9.27\n", "[2.5, 78.0, -12.4, 44.0, 8.21, -9.27]\n" ] } ], "source": [ "L=list(map(float,input(\"请输入一组数据,以空格分隔:\").split() ))\n", "print(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. 批量数据的经典算法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1)使用min,max,sum,len 可以求元组或列表中数值数据的最大值,最小值,累和,平均值。 算法思路,先构建列表,在使用序列函数计算" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=[]\n", "x=input('input a number ,over quit:')\n", "while x!=\"over\":\n", " a.append(float(x)) #a+=[float(x)]\n", " x=input('input a number ,over quit:')\n", "n=len(a)\n", "if n==0:\n", " print(\"没有输入\")\n", "else:\n", " s=sum(a)\n", " print('n=',n,'sum=',s,'average=',s/n)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2)获取最大值的位置,数据是有重复的,输出最大值和所有的下标位置 \n", "例如:34,98,57,72,98,0,16,32,98 \n", "输出:最大值98,位置:[1,4,8] " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入一组数据,以逗号分隔:34,98,57,72,98,0,16,32,98\n", "最大值98.0,位置:[1, 4, 8]\n" ] } ], "source": [ "L=list(map(float,input(\"请输入一组数据,以逗号分隔:\").split(\",\") ))\n", "maxindex=[0]\n", "for i in range(1,len(L)):\n", " if L[i]>L[maxindex[0]]:\n", " maxindex=[i]\n", " elif L[i]==L[maxindex[0]]:\n", " maxindex.append(i)\n", "print(f\"最大值{L[maxindex[0]]},位置:{maxindex}\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#请自行完成:获取最大值的位置,数据是有重复的,输出最大值和第一个下标位置\n", "#maxindex是一个单变量" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(3) 重复数据的查找" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "在一个序列中查找指定的值 x,如果找到则显示在序列中位置,如果没有找到,显示文字信息,查找的数据在列表中可能有多个。\n", "\n", "1.输入列表 L\n", "2.输入查找数x\n", "3.创建查找值下标列表Lx\n", "4.循环 i 从 0 到 len(L)-1,如果 L[i]等于 Lx 则 追加 i 到 Lx\n", "5.如果 len(Lx) 不等于 0 输出 Lx,否则输出”没有找到”" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import randint\n", "L=[ randint(10,20) for i in range(10)] #生成 10 个 10~20 的随机整数列表\n", "d={} #生成随机数重复次数字典\n", "for x in L:\n", " d[x]=_____(1)_____\n", "\n", "print(\"随机数列表:\",L)\n", "print(\"随机数出现次数:\",d)\n", "#输入查找数 x,显示在列表中的位置,第一个元素的位置为 1 \n", "x=int(input(\"输入查找数:\"))\n", "Lx= _____(2)_____\n", "for i in range(len(L)):\n", " if_____(3)_____:\n", " Lx._____(4)_____\n", "if_____(5)_____:\n", " print(\"没有找到\")\n", "else:\n", " print(\"出现位置\",Lx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(4) 二分查找" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "在一张有序的列表中使用二分查找法(折半查找)查找指定值,统计应用查找表中的每一个元素所需的查找次数。\n", "\n", "1.设置 left 为 0,right 为 len(L)-1\n", "2.循环当 left 小于等于 right\n", " 2.1\t计算 mid\n", " 2.2\t如果 target \n", " 等于 L[mid] 则 跳出循环否则如果 target 大于 L[mid] 则 left=mid+1\n", " 否则 right=mid-1\n", "3.如果 left>right 则没有找到\n", " 否则 找到,x 的位置是 mid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L=[20, 25, 30, 35, 40, 45, 50, 55] # 有序顺序表\n", "Lc=[] # 每个元素所查找次数列表\n", "for ____(1)____ in L: # 遍历 L 中每一个元素\n", " left,right= _____(2)_____ # 搜索区域初始化\n", " c=0 #查找次数计数器清零\n", " while _____(3)_____: #二分查找算法\n", " mid=(left+right)//2 \n", " c+=1\n", " if target==L[mid]:\n", " Lc.append(c) #找到,记录查找次数\n", " _____(4)_____\n", " elif target>L[mid]:\n", " left=mid+1\n", " else:\n", " _____(5)_____\n", "print(dict(zip(L,Lc)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(5) 排序问题\n", "\n", "sorted函数是python内置函数,直接使用,排序后产生一个新的列表。函数格式如下: \n", "```python\n", "sorted(iterable, /, *, key=None, reverse=False)\n", "\titerable: 需要排序的迭代序列,可以是列表、元组、字典等\n", "\tkey:参数是一个函数,在每个数据项比较前被调用,决定排序关键字。\n", "\treverse:True为降序,False(默认)为升序\n", "```" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 5, 7, 8, 9]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L=[5,3,1,8,7,9,0,2]\n", "sorted(L)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 8, 7, 5, 3, 2, 1, 0]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(L,reverse=True)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['is', 'Mr', 'the', 'This', 'from', 'Song', 'letter']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#使用key参数设置排序规则\n", "sorted(\"This is the letter from Mr Song\".split(),key=len)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('112', 'Mary', 90),\n", " ('127', 'Lily', 95),\n", " ('141', 'David', 90),\n", " ('158', 'sara', 80)]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#设置按学号排列\n", "stus=[('141','David',90),('112','Mary',90),('158','sara',80),('127','Lily',95)]\n", "sorted(stus,key=lambda x:x[0]) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\t匿名函数lambda的格式如下:\n", "```python\n", "lambda 参数列表:表达式\n", "匿名函数,没有函数名。冒号前是参数,冒号后是表达式,表达式计算的结果是函数的返回值。\n", "\t排序关键字的确定:key=lambda x:x[0]\n", "x是嵌套列表中的一个数据项,lambda函数返回值是x[0],则key参数设置了以嵌套列表子项中的第一项学号为排序关键字。\n", "```" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[10170110313, 1500, 300],\n", " [10170110310, 1500, 500],\n", " [10170110308, 1500, 800],\n", " [10170110311, 2100, 600],\n", " [10170110307, 2700, 300],\n", " [10170110309, 2500, 600],\n", " [10170110314, 2500, 600],\n", " [10170110312, 3500, 200]]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# s给出了工号,基本工资和奖金,设置按总收入排序\n", "s=[[10170110307,2700,300],[10170110308,1500,800],[10170110309,2500,600],\\\n", "[10170110310,1500,500],[10170110311,2100,600],[10170110312,3500,200],\\\n", " [10170110313,1500,300],[10170110314,2500,600]]\n", "sorted(s,key = lambda x :x[1]+x[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 小试身手2\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. 使用随机函数randint(),建立两个长度为5的二维列表A和B。包含的数据如下:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "A列表:包含子列表 [学号,p成绩,m成绩],学号为从1开始递增的正整数,成绩为0-100之间的随机整数。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "B列表:包含子列表 [学号,q成绩],学号为从1开始递增的正整数,成绩为0-100之间的随机整数‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "先需要将A,B两个列表数据进行合并,即将B列表中的‘q成绩’添加到A列表中的相同学号的子列表中。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "输入一个整数k,将B列表中的'q成绩'插入到A列表相同学号的子列表的k位置上。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "合并完成后,输出合并后的A列表。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬\n", "\n", "注意:为保证生成固定序列,本题需要使用同一个循环结构生成两个随机列表A和B,类似:\n", "for #######:\n", " #A列表添加 学号,p成绩,m成绩\n", " #B列表添加 学号,q成绩\n", "运行示例:\n", "A: [[1, 94, 58], [2, 6, 84], [3, 26, 42], [4, 39, 98], [5, 22, 18]]\n", "B: [[1, 33], [2, 82], [3, 29], [4, 26], [5, 24]]\n", "3\n", "[[1, 94, 58, 33], [2, 6, 84, 82], [3, 26, 42, 29], [4, 39, 98, 26], [5, 22, 18, 24]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "a=[]\n", "b=[]\n", "\n", "for i in range(1,6): \n", " ___________【1】____________________________ \n", " ___________【2】___________________________\n", "print(\"A:\",a)\n", "print(\"B:\",b)\n", "p=int(input()) \n", "for i in range(len(a)):\n", " a[i].______【3】_____________ \n", "print(a) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2)成绩统计\n", "输入用空格分隔的两个正整数n和m,表示后面要接收m个人的n门课的成绩的输入。\n", "随后的m行,每行输入用空格分隔的n个成绩。题目保证m和n都在(0-100]之间。\n", "请统计\n", "(1)每门课的平均成绩。\n", "(2)每个学生的平均成绩\n", "(3)每门课的最高分" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 2 }