博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
6.3 迭代 递归
阅读量:6658 次
发布时间:2019-06-25

本文共 3253 字,大约阅读时间需要 10 分钟。

迭代器

count = 0 while True:     if count ==101: break print(count) count += 1

python中一切皆对象

height = 180 salary = 3.2 name = 'nick' hobby_list1 = ['run','read'] hobby_tup = ('run','read') info_dict1 = { 'name':'nick','weight':140} hobby_set = { 'read','run','run'} def func(): pass # w和a模式都会自动生成文件 f = open('test.txt','w',encoding='utf8')

可迭代对象:只要拥有iter方法的对象就是可迭代对象

height.__iter__ salary.__iter__ func.__iter__ name.__iter__() hobby_list.__iter__() hobby_set.__iter__() hobby_tup.__iter__() info_dict.__iter__() f.__iter__()

字符串、列表、元祖、字典、集合、文件都是可迭代对象

hobby_list = ['run','read'] hobby_list_iter = hobby_list.__iter__() # 把列表变成可迭代对象 # print(hobby_list_iter.__next__()) # print(hobby_list_iter.__next__()) # print(hobby_list_iter.__next__()) # for k in info_dict: # print(k) info_dict = { 'name':'nick','weight':140} info_dict_iter = info_dict.__iter__() # print(info_dict_iter.__next__()) # print(info_dict_iter.__next__()) # print(info_dict_iter.__next__())

next其实是在遍历和迭代对象的元素,一旦遍历完报错

迭代器对象:拥有 iter _ 方法,可迭代对象拥有next_方法的才是迭代器对象,文件本身就是迭代器对象

hobby_list2 = ['run','read'] # count = 0 # while True: # print(hobby_list2[count]) # # count += 1 # # if count == len(hobby_list2): # break # for循环不依赖索引取值 # 这一段代码如果用c写,就是for循环的原理 hobby_list2 = ['run','read'] hobby_list2_iter = hobby_list2.__iter__() while True: try: print(hobby_list2_iter.__next__()) except: break for i in hobby_list2: # hobby_list2,把hobby_list2转化为可迭代对象 print(i) print(hobby_list2) # 一筐鸡蛋 print(hobby_list2.__iter__()) # 相比较列表,它节省内存空间,老母鸡 print(hobby_list2_iter) print(hobby_list2_iter.__iter__().__iter__().__iter__()) # 迭代器对象使用iter方法后是迭代器对象本身

可迭代对象:拥有iter方法的对象就是可迭代对象,推到:可迭代对象不一定是迭代器对象

迭代器对象:拥有iter方法的和next方法的就是迭代器对象,推导:迭代器对象一定是可迭代对象

文件即可迭代对象,又是迭代器对象

 

三元表达式(三目表达式)

dog_name = 'xiaogou' ​ if dog_name =='fenggou':     print('远离他') else: print('盘他') #不推荐使用 print('远离他') if dog_name =='fenggou' else print('盘他') # 列表推导式 # lis = [] # # for i in range(100): # lis.append(i) # # print(lis) # lis = [i*2 for i in range(100)] # print(lis) # 千万不要写这样的东西,否则真的会被骂傻逼 lis1 = [i * 2 if i > 50 else i for i in range(100)] print(lis1)

字典生产式

​ lis = [i for i in range(10)] print(lis) dic2= dict.fromkeys([1,2,3,4],2) print(dic2) dic = { i:i**2 for i in range(10)} for i in dic.items(): print(i) 拉链函数 res = zip('abcd',[1,2,3,4]) dic = dict() for k,v in res: dic[k] = v print(dic) print({ k:v for k,v in zip('abcd',[1,2,3,4])})

递归

def f2():     print('from f2')      # 递归: 函数掉函数自己,类似于循环,但是这个循环必须有结束条件 import time def f1(x): # x=0 print(x) # 0 1 # time.sleep(1) x += 1 # 1 2 if x ==101: return f1(x) # x=1 def f2(): f1() f1(0) def guess_age(age,count): age -= 2 count -= 1 if count ==1: print(age) return guess_age(age,count) guess_age(38,5) def guess_age(count): #age ==38 count =5 #age -=2 #36.34.32.30 count -= 1 if count ==1: # print(age) return 26 return guess_age(count) +2 res = guess_age(5) print(res)
def guess_age(count):# 5     count -= 1# 4 if count ==1: return 26 return guess_age(count) +2# guess_age(4) + 2 # guess_age(3) + 2 # guess_age(2) + 2 # guess_age(5) = guess_age(4) + 2 = (guess_age(3) + 2) + 2 = ((guess_age(2) + 2) + 2) + 2 = 26 + 2 + 2 + 2 = 32 # 26 + 2 =guess_age(3)=28 # 28+2=guess_age(4) = 30 # 30 + 2 res = guess_age(5) print(res)

 

转载于:https://www.cnblogs.com/zrx19960128/p/10970137.html

你可能感兴趣的文章
Heroku error:Permission denied (publickey)
查看>>
解决linux的-bash: ./xx: Permission denied
查看>>
Laravel 第三方登陆之 Socialite Providers
查看>>
Ubuntu14.10 remove ibus 之后
查看>>
Spring第一天
查看>>
功能需求流程、流程图制作软件
查看>>
linux 下C语言线程示例 + 线程同步
查看>>
在Fedora、opensuse下删除默认安装的openjdk,并安装oracle jdk的方法
查看>>
springMVC笔记系列(20)——控制器实现详解(下)
查看>>
【软件周刊】Google Chrome 55 发布,默认禁用 Flash;PHP 7.1.0 发布,提供新的语法...
查看>>
guava-布隆过滤器
查看>>
【笔记】浏览器同源策略二三事
查看>>
人脸识别系统原理
查看>>
Linux文件上传下载,rz和sz
查看>>
在as3中使用嵌入字体
查看>>
Flink VS Spark
查看>>
cxf集成到spring中发布restful webservice
查看>>
linux系统调用出错时的处理函数
查看>>
How processor, assembler, and programming langu...
查看>>
cocos2d-x 异步加载plist,png等文件
查看>>