sequence! sequence! + Exercise Review

##sequence! sequence!
           ※ list, tuples and strings in common
               - each element can be obtained by the index
               - from the default index value is always zero
               - can be set within a range of elements by the method of fragmentation
               - there are many common operations character (repetition operator, splicing operators, relational operators members)
            (so the list of tuples, and strings are collectively referred to as sequences)

## sequences in common BIF
           ※ List (): the role of the iterable into a list
              ※ iterations: the so-called iterative feedback process is repeated activities, its purpose is usually to close and achieve the desired goal or result each process was repeated once we called an iteration, each iteration and the results obtained will be used as an initial value for the next iteration, so that now, for the iteration is a loop,
            (List () as the method itself it has two forms, one form is parameter is not a parameter, the parameter he did not generate a default empty empty list, there is another iterable a parameter, which is an iterator)
Here Insert Picture Description
            (a tuple of the variant It became a list)
Here Insert Picture Description

           ※ tuple ([iterable]): the role of an iterator object into a tuple, implementation and list () is the same as most

           ※ str (obj): a string into the object obj

           ※ len (sub): returns the length of the parameter sub

           ※ max (): returns the maximum value of the sequence parameter set, or

            (Directly to value)
            Here Insert Picture Description
            (it may also be a transmission sequence, since b is a character list, the maximum value of the returned code is a relatively large ASCII)
Here Insert Picture Description
            Here Insert Picture Description

           ※ min (): returns the sequence parameter set or a minimum value of
           Here Insert Picture Description
            (a string can pass relatively Comparative ASCII code)
            Here Insert Picture Description
            (can also pass a tuple values are compared, it is to be noted that the method used max and min are to ensure that the data type or the parameter sequence are uniform, are all characters can be are all integers, floating point tuple)
            Here Insert Picture Description
           ※ SUM (iterable [, start = 0]): return sequence and optional parameter start iterable Sum
           Here Insert Picture Description

            (Incoming string can not be summed
            Here Insert Picture Description
           ※ sorted (): returns a sorted list, the default is from small to large and the list of built-in functions list.sort () is the same
           Here Insert Picture Description

           ※ reversed (): a list of the in situ reversed, and a list of built-in functions list.reverse () is the same, but it is not return a list, but an iterator object, can be converted into an indirect method list listing
           Here Insert Picture Description
           ※ enumerate (): index value generated by the tuple (index) value of each element and composition of the item, and then inserted into the list, returns an enumeration object
Here Insert Picture Description
           ※ zip (): returns a sequence of individual composition parameters tuples
           Here Insert Picture Description

## of refresher exercises
           0.5 based on common characteristics of our lists, tuples and strings, put them three collectively Why?
           A: sequence, because they have the following in common:
           1) can get each element by index
           2) default index values are always zero (of course flexible Python also supports negative indexes
           3) can be obtained by a method of fragmentation set of elements in the range
           4) share many operators (operator repeated, splicing operators, relational operators members

           1. 请问分别使用什么BIF,可以把一个可迭代对象转换为列表、元祖和字符串?
           答:list([iterable]) 把可迭代对象转换为列表
           tuple([iterable]) 把可迭代对象转换为元组
           str(obj) 把对象转换为字符串
           例如:

1. >>> temp = 'I love FishC.com!' 
2. >>> list(temp) 
3. ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'C', '.', 'c', 'o', 'm', '!'] 

           2. 你还能复述出“迭代”的概念吗?
           答:所谓迭代, 是重复反馈过程的活动, 其目的通常是为了接近并到达所需的目标或结果。 每一次对过程的重复被称为一次“迭代”, 而每一次迭代得到的结果会被用来作为下一次迭代的初始值。

           3. 你认为调用 max(‘I love FishC.com’) 会返回什么值?为什么?
           答:会返回: ‘v’ ,因为字符串在计算机中是以 ASCII 码的形式存储( ASCII 对照表:http://bbs.fishc.com/thread-41199-1-1.html ),参数中 ASCII 码值最大的是 ‘v’ 对应的118 。

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

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

Here Insert Picture Description
##动动手
           0. 猜想一下 min() 这个BIF的实现过程
           答:

def min(x):
    least = x[0]           #先把最小值给least
    for each in x:
        if each < least:    
            least = each   #比最小值还小就更新least的值
    return least
print(min('123456789')) 

Here Insert Picture Description
           注:关于函数的定义和使用在下一讲的课程中讲解, 目前只需要理解该 BIF 实现的原理即可。

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

def sum(x):
    result = 0
    for each in x:
        if(type(each) == int)or(type(each)==float):
            result +=each
        else:
            continue
        
    return result

print(sum([1, 2.1, 2.3, 'a', '1', True])) 

Here Insert Picture Description

Published 161 original articles · won praise 70 · views 50000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104059227