zoukankan      html  css  js  c++  java
  • 第016讲:序列!序列! (课后测试题及答案 )

    一、列表、元组和字符串的共同点

    1.都可以通过索引得到每一个元素

    2.默认索引值总是从0开始

    3.可以通过分片的方法得到一个范围内的元素的集合

    4.有共同的操作符(重复操作符、成员关系操作符、拼接操作符)

    二、序列常见的BIF

    1.list():把一个可迭代对象转化为一个列表

    list本身作为一个方法,有2中形态:无参数、有参数

    无参数,就是生成一个空的列表

    >>> list()
    []
    >>> 

    有参数

    >>> a = 'I love Fishc.com'
    >>> a = list(a)
    >>> a
    ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'c', '.', 'c', 'o', 'm']
    >>> b = (1,1,2,3,5,8,13,21,34)
    >>> b = list(b) #元组转化为列表
    >>> b
    [1, 1, 2, 3, 5, 8, 13, 21, 34]
    >>> 

    2.tuple():把一个可迭代对象转化为元组

    >>> b
    [1, 1, 2, 3, 5, 8, 13, 21, 34]
    >>> b = tuple(b)
    >>> b
    (1, 1, 2, 3, 5, 8, 13, 21, 34)
    >>> 

      >>> str1 = 'I love Fishc.com'
      >>> tuple(str1)
      ('I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'c', '.', 'c', 'o', 'm')
      >>>

    3.str()

    >>> b
    (1, 1, 2, 3, 5, 8, 13, 21, 34)
    >>> b = str(b)
    >>> b
    '(1, 1, 2, 3, 5, 8, 13, 21, 34)'
    >>>

      >>> b = [1,2,3,4]
      >>> str(b)
      '[1, 2, 3, 4]'
      >>>

    4.len(sub):返回参数sub的长度

    5.max():返回序列或者参数集合中的最大值

    >>> max('a','o','v','e') #V的ASCII码是最大的
    'v'
    >>> numbers = [1,34,45,-98,0,98]
    >>> max(numbers)
    98
    >>> 

    >>> max((1,2,3,4))
    4
    >>> max([4,2,3,1])
    4
    >>>

    >>> max([1,2,3],[3,4,5],[0,5,3])#max参数的中列表比大小,比list[0]的大小
    [3, 4, 5]
    >>> max([1,2,3],[3,4,5],[4,0,0])
    [4, 0, 0]

    6.min():返回序列或者参数集合中的最小值

    >>> numbers = [1,34,45,-98,0,98]
    >>> min(numbers)
    -98
    >>> chars = '1234567890'  #数字字符串
    >>> min(chars)            #字符串的0的ASCII最小,是30,这个点要注意
    '0'
    >>> 

      >>> min((1,2,3,4,5))
      1
      >>> min([2,3,4,1,5,6])  
      1

    >>> min([1,2,3],[3,4,5],[4,0,0])
    [1, 2, 3]
    >>>

    >>> a = [1,3,4,6,9,7,'a']
    >>> max(a)
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        max(a)
    TypeError: '>' not supported between instances of 'str' and 'int'
    >>> 

    7.sum(iterable[,start = 0]):返回序列参数iterable 和可选参数start的总和.

    >>> tuple1 = (1.2,4.5,6.7)
    >>> sum(tuple1)
    12.4
    >>> sum(tuple1,10)
    22.4
    >>> chars
    '1234567890'
    >>> sum(chars)
    Traceback (most recent call last):
      File "<pyshell#27>", line 1, in <module>
        sum(chars)
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> 
    >>> sum(3,5,7)
    Traceback (most recent call last):
      File "<pyshell#51>", line 1, in <module>
        sum(3,5,7)
    TypeError: sum() takes 1 positional argument but 3 were given
    >>> sum([3,4,5])
    12
    >>> 

    注意:

    1.使用max()、min()、sum()方法,参数中的数据类型必须是一致的,否则会报错,不支持不同类型的比较、相加。

    2.上述都是序列的BIF,参数必须是一个序列,下面将的内置函数也是序列的BIF,

    >>> sorted(1,3,2,5)
    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in <module>
    sorted(1,3,2,5)
    TypeError: sorted expected 1 argument, got 4
    >>> sorted([1,3,2,5])
    [1, 2, 3, 5]
    >>>

    8.sorted():返回一个排序的列表(默认是从小到大排序)

    sort()是列表的方法,使用时list.sort(),而sorted()是序列的内置函数

    >>> num = [2,6,8,34,65,34,56,23,-98,45]
    >>> sorted(num)
    [-98, 2, 6, 8, 23, 34, 34, 45, 56, 65]
    >>> 

    9.reversed():返回一个序列的翻转

    reverse()是列表的方法,使用时list.reverse(),而reversed()是序列的内置函数

    >>> num
    [2, 6, 8, 34, 65, 34, 56, 23, -98, 45]
    >>> reversed(num)
    <list_reverseiterator object at 0x02B52D48>
    >>> list(reversed(num))
    [45, -98, 23, 56, 34, 65, 34, 8, 6, 2]
    >>>

    10.enumrate():枚举,生产由每个元素的index值和item值组成的元组

    >>> num
    [2, 6, 8, 34, 65, 34, 56, 23, -98, 45]
    >>> enumerate(num)
    <enumerate object at 0x02B32188>
    >>> list(enumerate(num))
    [(0, 2), (1, 6), (2, 8), (3, 34), (4, 65), (5, 34), (6, 56), (7, 23), (8, -98), (9, 45)]
    >>> 

    11.zip():返回由各个参数的序列组成的元组

    >>> a = [1,2,3,4,5,6,7,8]
    >>> b =[9,8,7,6]     #列表b没有的,列表a直接省去
    >>> list(zip(a,b))  #将a,b打包
    [(1, 9), (2, 8), (3, 7), (4, 6)]   
    >>> 

    课后习题:

    测试题

    0. 我们根据列表、元组和字符串的共同特点,把它们三统称为什么?

         序列

    1. 请问分别使用什么BIF,可以把一个可迭代对象转换为列表、元组和字符串?

        list()、tuple()、str()

    2. 你还能复述出“迭代”的概念吗?

        所谓迭代,是重复反馈过程的活动,其目的通常是为了接近并到达所需的目标或结果。每一次对过程的重复被称为一次“迭代”,而每一次迭代得到的结果会被用来作为下一次迭代的初始值。

    3. 你认为调用 max('I love FishC.com') 会返回什么值?为什么?

    会返回:'v',因为字符串在计算机中是以ASCII码的形式存储,参数中ASCII码值最大的是'v'对应的118。

    >>> max('I love FishC.com')
    'v'
    >>> 

    4. 哎呀呀,现在的小屁孩太调皮了,邻居家的孩子淘气,把小甲鱼刚写好的代码画了个图案,麻烦各位鱼油恢复下啊,另外这家伙画的是神马吗?怎么那么眼熟啊!??

    动动手

    name = input('请输入待查找的用户名:')
    score = [['迷途', 85], ['黑夜', 80], ['小布丁', 65], ['福禄娃娃', 95], ['怡静', 90]]
    IsFind = False
     
    for each in score:
        if name in each: #if name == each[0]:也可以
            print(name + '的得分是:', each[1])
            IsFind = True
            break
        
    if IsFind == False:
        print('查找的数据不存在!')

    0. 猜想一下 min() 这个BIF的实现过程

    def min(x):
        least = x[0]
        for each in x:
            if each < least:
                least = each
                
        return least
    
    print(min('5679335'))

    1. 视频中我们说 sum() 这个BIF有个缺陷,就是如果参数里有字符串类型的话就会报错,请写出一个新的实现过程,自动“无视”参数里的字符串并返回正确的计算结果

    #sum functions
    def sum(x):
        results = 0
        for each in x:
            if (type(each)==int) or (type(each)==float):
                results += each
            else:
                continue
        return results
    print(sum([2,3,4,'r','6',7.8,9.8]))

    2. 请写下这一节课你学习到的内容:格式不限,回忆并复述是加强记忆的好方式!

  • 相关阅读:
    苹果将首次采用HTML5直播发布会 狼人:
    Python 3.2 alpha 2发布 狼人:
    下一代Linux文件系统Btrfs走向成熟 狼人:
    Hello! 404 狼人:
    退格回车控制台输入密码
    poj 3233 Matrix Power Series
    地址参考clang: error: linker command failed with exit code 1 (use v to see invocation)
    文本截断JQuery为textarea添加maxlength,并且兼容IE
    代码下载Html5初探视频元素video示例
    c# 限制textbox的输入范围和长度(长度不用maxlength方法)
  • 原文地址:https://www.cnblogs.com/ananmy/p/12093206.html
Copyright © 2011-2022 走看看