标签:不能 ati 并且 内容 索引 print local 空间 记录
# 可迭代对象
# 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)