• 11-python-iterator


    # 可迭代对象
    # python中一切皆对象,一个实实在在存在的值或者内容
    # 可迭代对象:可以进行循环更新的一个实实在在值(字面意思)
    # 内部含有'__iter'方法的对象就叫做可迭代对象
    # s1 = 'fasd'
    # s2 = dir(s1)
    # if '__iter__' in s2:
    # print('true')
    # else:
    # print('false')
    # print('__iter__' in dir(s1))
    # print('__iter__' in dir(range(10)))

    # 小结:可迭代对象:判断 --> print('__iter__' in dir(range(10)))
    # 优点:
    # 1、存储的数据直接能显示,比较直观
    # 2、拥有方法多,操作方便
    # 缺点:
    # 1、占用内存
    # 2、不能直接通过for循环,不能直接取值(索引、key)

    # 迭代器:可更新迭代的工具(字面意思)
    # 内部含有__iter__方法并且含有__next__方法的对象就是迭代器
    # f = open('../day002/333.txt')
    # print('__iter__' in dir(f) and '__next__' in dir(f))

    # 可迭代对象可以转化成迭代器
    # s1 = 'asdiuqyw'
    # obj = iter(s1) # s1.__iter__()
    # print(obj)
    # print(obj.__next__()) # next(obj)
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())
    # print(obj.__next__())

    # l1 = [11,22,33,44,55]
    # obj = iter(l1)
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))

    # l1 = [11,22,33,44,55,66,77,88,99,00]
    # obj = iter(l1)
    # for i in range(4):
    # print(next(obj))
    # for i in range(4):
    # print(next(obj))

    # 小结:
    # 迭代器
    # 优点:1、节省内存;2、惰性机制next只取一个;
    # 缺点:1、速度慢;2、不走回头路
    # 迭代器与可迭代对象
    # 可迭代对象是一个操作方法比较多,比较直观,存储数据相对少(几百万个对象,8G内存是可以承受的)的一个数据集。
    # 数据需要灵活处理,且内存空间足够,将数据机设置为可迭代对象是明确的选择。
    # 迭代器节省内存,可以记录取值位置,可以通过__next__()方法取值,不直观,操作方法比较单一的数据集
    # 数据量大就用迭代器。

    # 利用while循环模拟for循环对可迭代对象进行取值的机制
    # l1 = [1,2,3,4,5]
    # obj = iter(l1)
    # while 1:
    # try:
    # print(obj.__next__())
    # except StopIteration:
    # break

    # homework
    # 请写出下列代码的执行结果:
    # 例一:
    # def func1():
    # print('in func1')
    # def func2():
    # print('in func2')
    # ret = func1
    # ret() # in func1
    # ret1 = func2
    # ret1() # in func2
    # ret2 = ret #
    # ret3 = ret2 #
    # ret2() # in func1
    # ret3() # in func1

    # ​例二:
    # def func1():
    # print('in func1' )
    # def func2():
    # print('in func2' )
    # def func3(x, y):
    # x()
    # print('in func3' )
    # y()
    # print(111) # 111
    # func3(func2, func1) # in func2 in func 3 in func1
    # print(222) # 222

    # ​ 例三(选做题):
    #
    # def func1():
    # print('in func1')
    # def func2(x):
    # print('in func2')
    # return x
    # def func3(y):
    # print('in func3')
    # return y
    # ret = func2(func1) # in func2
    # ret() # in func1
    # ret2 = func3(func2) # in func3
    # ret3 = ret2(func1) # in func2
    # ret3() # in func1

    # 执行结果:
    # 看代码写结果:
    # def func(arg):
    # return arg.replace('苍老师', '***')
    # def run():
    # msg = "Alex的女朋友苍老师和大家都是好朋友"
    # result = func(msg)
    # print(result)
    # run() # "Alex的女朋友***和大家都是好朋友"
    #
    # def func(arg):
    # return arg.replace('苍老师', '***')
    # def run():
    # msg = "Alex的女朋友苍老师和大家都是好朋友"
    # result = func(msg)
    # print(result)
    # data = run() # "Alex的女朋友***和大家都是好朋友"
    # print(data) # None

    # DATA_LIST = []
    # def func(arg):
    # return DATA_LIST.insert(0, arg)
    # data = func('绕不死你')
    # print(data) # [绕不死你] --
    # print(DATA_LIST) # [] --

    # 看代码写结果:
    # def func():
    # print('你好呀')
    # return '好你妹呀'
    # func_list = [func, func, func]
    # for item in func_list:
    # val = item()
    # print(val)
    '''
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    '''
    # 看代码写结果:
    # def func():
    # print('你好呀')
    # return '好你妹呀'
    # func_list = [func, func, func]
    # for i in range(len(func_list)):
    # val = func_list[i]()
    # print(val)
    '''
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    '''

    # 看代码写结果:
    # def func():
    # return '烧饼'
    # def bar():
    # return '豆饼'
    # def base(a1, a2):
    # return a1() + a2()
    # result = base(func, bar)
    # print(result) # 烧饼豆饼

    # 看代码写结果:
    # for item in range(10):
    # print(item)
    # print(item)
    # 1-9 9

    # 看代码写结果:
    #
    # def func():
    # for item in range(10):
    # pass
    # print(item)
    # func()
    # #
    # 看代码写结果:
    # item = '老男孩'
    #
    #
    # def func():
    # item = 'alex'
    #
    # def inner():
    # print(item)
    #
    # for item in range(10):
    # pass
    # inner()
    #
    #
    # func()
    #
    # 看代码写结果:
    # l1 = []
    # def func(args):
    # l1.append(args)
    # return l1
    # print(func(1)) # [1,]
    # print(func(2)) # [1,2]
    # print(func(3)) # [1,2,3]
    # print(l1)
    #
    # 看代码写结果:
    # name = '太白'
    #
    #
    # def func():
    # global name
    # name = '男神'
    #
    #
    # print(name)
    # func()
    # print(name)
    #
    # 看代码写结果:
    # name = '太白'
    #
    #
    # def func():
    # print(name)
    #
    #
    # func()
    #
    # 看代码写结果:
    # name = '太白'
    #
    #
    # def func():
    # print(name)
    # name = 'alex'
    #
    #
    # func()
    #
    # 看代码写结果:
    #
    # def func():
    # count = 1
    #
    # def inner():
    # nonlocal count
    # count += 1
    # print(count)
    #
    # print(count)
    # inner()
    # print(count)
    #
    #
    # func()
    #
    # 看代码写结果:
    #
    def extendList(val, list=[]):
    list.append(val)
    return list


    # list1 = extendList(10) # [10,]
    # list2 = extendList(123, [])
    # list3 = extendList('a')
    #
    # print('list1=%s' % list1)
    # print('list2=%s' % list2)
    # print('list3=%s' % list3)

    # 看代码写结果:
    #
    # def extendList(val, list=[]):
    # list.append(val)
    # return list
    #
    #
    # print('list1=%s' % extendList(10))
    # print('list2=%s' % extendList(123, []))
    # print('list3=%s' % extendList('a'))
    #
    # 用你的理解解释一下什么是可迭代对象,什么是迭代器。
    # 如何判断该对象是否是可迭代对象或者迭代器?
    # 写代码:用while循环模拟for内部的循环机制(面试题)。
    # 写函数,传入n个数,返回字典
    # {‘max’:最大值,’min’:最小值}
    # 例如: min_max(2, 5, 7, 8, 4)
    # 返回: {‘max’:8,’min’:2}(此题用到max(), min()内置函数)
    # print(max([1,2,3,4]))
    # max
    # def func(*args):
    # return {'max':max(args),'min':min(args)}
    # print(func(1,2,100,123,45,6,7,89))

    # 写函数,传入一个参数n,返回n的阶乘
    # 例如: cal(7)
    # 计算7*6*5*4*3*2*1
    #
    # def func(num):
    # count = 1
    # for i in range(num, 0, -1):
    # count = count * i
    # func(10)

    # 写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组(选做题)
    # 例如:[(‘红心’,2), (‘草花’,2), …(‘黑桃’,‘A’)]

    # 写代码完成99乘法表.(选做题,面试题)
    # 1 * 1 = 1
    #
    # 2 * 1 = 2
    # 2 * 2 = 4
    #
    # 3 * 1 = 3
    # 3 * 2 = 6
    # 3 * 3 = 9
    #
    # ......
    #
    # 9 * 1 = 9
    # 9 * 2 = 18
    # 9 * 3 = 27
    # 9 * 4 = 36
    # 9 * 5 = 45
    # 9 * 6 = 54
    # 9 * 7 = 63
    # 9 * 8 = 72
    # 9 * 9 = 81
  • 相关阅读:
    vue改变了数据却没有自动刷新
    Unable to find vcvarsall.bat
    修改Linux用户配置之后先验证再退出
    平面最远点对
    [转]你可能不知道的五个强大HTML5 API
    sqlite3常用技巧
    使用rsync
    画图必备numpy函数
    np.percentile获取中位数、百分位数
    [转]numpy 100道练习题
  • 原文地址:https://www.cnblogs.com/Daspig/p/12818876.html
Copyright © 2020-2023  润新知