08 条件与循环,迭代器,列表解析,生成器(笔记)
来源:互联网 发布:手机淘宝店面 编辑:程序博客网 时间:2024/06/17 03:28
1. python采用缩进规则,可以有效避免dangling else
2. 条件表达式(三元操作符)t=x if condition1 else y
3. pass语句表示不做任何操作,即no operation
4. 对于一个很大范围的列表,xrange()比range()更适合,因为xrange()不会在内存中创建列表的完整拷贝
迭代器:
为类序列对象提供了一个类序列接口,可以遍历任何表示出序列行为的对象,比如字典key,文件行等。采用next()方法来遍历条目,结束后会抛出StopIteration一次,对于for循环方法,就是内部调用next()方法,并捕获异常。
- 序列
>>> s=[1,2,3]
>>> c=iter(s)
>>> c.next()
1
>>> c.next()
2
>>> c.next()
3
>>> c.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> s
[1, 2, 3]
>>> while True:
... try:
... print i.next()
... except StopIteration:
... break
...
1
2
3
>>> dic={"x":1,"y":2,"z":3}
>>> i=dic.iterkeys()
>>> i.next()
'y'
>>> i.next()
'x'
>>> i.next()
'z'
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> for i in dic:
... print i
...
y
x
z
>>> dic
{'y': 2, 'x': 1, 'z': 3}
>>> for k in dic.keys():#返回独立列表
... print k, dic[k]
... del dic[k]
...
y 2
x 1
z 3
>>> dic
{}
>>> l=[1,2,3]
>>> for i in l:
... print i
... l.remove(i)
...
1
3
>>> l
[2]
>>> l=[1,2,3]
>>> i=iter(l)
>>> c=i.next()#指向index 0
>>> l.remove(c)
>>> i.next() #指向index 1
3
>>> dic
{'y': 2, 'x': 1, 'z': 3}
>>> for k in dic:#与实体对象挂钩,iterator指向下一个key,而不是某个index,此时删除key,iterator根据不知道下一个该指向哪里
... print k,dic[k]
... del dic[k]
...
y 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
>>> dic
{'x': 1, 'z': 3}
>>> [(x+1,y+1) for x in range(3) for y in range(5)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]
It’s one of my most favorite cartoon during my childhood.
What about you?
>>> import os
>>> os.stat("t.txt").st_size
136
>>> sum([len(word) for line in open("t.txt","r") for word in line.split()])
113
>>> len([word for line in open("t.txt","r") for word in line.split()])
23
>>> sum([int(raw_input()) for i in range(5)])
1
2
3
4
5
15
>>> sum(len(word) for line in open("t.txt","r") for word in line.split())
113
- 交叉配对
利用生成器的惰性分析,我们可以一个一个处理结果,此例中,我们采用输出处理
>>> l
[1, 2, 3]
>>> def cols():
... yield 4
... yield 5
... yield 6
...
>>> x=((i,j) for i in l for j in cols())
>>> x
<generator object <genexpr> at 0x1068bdcd0>
>>> for p in x:
... print p
...
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
"good example to recursive thought"
#the condition to stop
if N<3:
return 1
#f(N)=f(N-1)+f(N-2), e.g. f(3)=f(1)+f(2), f(4)=f(3)+f(2), etc..
return fibonacci(N-1)+fibonacci(N-2)
8-11: 这个解答中只是简单的判断是否以“,”隔开姓和名,并且姓和名的第一个字母是否是大写的
while True:
N=int(raw_input("Enter total number of names:"))
if N:
break
ls,count=[],0
for i in range(N):
s=raw_input("Please enter name %d: "%i)
if len(s.split(","))!=2 or s.split(",")[0].strip()[0].islower() or s.split(",")[1].strip()[0].islower():
count+=1
print "Wrong format... should be Last, First."
print "You have done this %d time(s) already. Fixing input..."%count
ls.append(s.replace(" ",",").lower().title())
else: ls.append(s.replace(" ",""))
print "sorted list (by last name) is:"
ls.sort()
for l in ls:
print l
- 08 条件与循环,迭代器,列表解析,生成器(笔记)
- 列表解析与生成器表达式
- Learning Python:1. 生成器函数、列表解析与map,for循环的效率对比
- Python基础:08列表解析与生成器表达式
- 列表解析式与生成器表达式
- Python 生成器函数,生成器表达式,迭代器,列表解析
- python--迭代器,生成器,三元表达式,列表解析,生成器表达式
- Python基础02--迭代器、生成器、列表解析
- <Python核心编程>条件和循环(包括迭代器、生成器)
- 笔记9列表生成器
- 列表生成器笔记(Python)
- python整理十——生成器表达式与列表解析
- Python列表解析和生成器
- 4 Python 迭代器_生成器_列表解析
- python条件与循环
- <PY><core python programming笔记>C8 条件与循环
- Python学习笔记——条件判断与循环
- python 学习笔记(2) 条件语句与循环语句
- 燕十八老师mysql高级篇代码(手敲)
- 10gR2 RAC Install issues on Oracle EL5 or RHEL5 or SLES10 (VIPCA / SRVCTL / OUI Failures) (Doc ID 41
- Android关于EditText和WindowManager的一些知识
- [小米] 并查集
- 工业4.0时代的质量工作十化
- 08 条件与循环,迭代器,列表解析,生成器(笔记)
- cocos2dx-lua 在cocos2d中使用lua脚本进行开发,并介绍脚本在游戏中的详细用途
- c++内存池的实现(可以new复杂对象)
- 智能制造不是筐
- Spring MVC JSR303数据校验
- 黑马程序员------oc语言学习笔记之类和对象
- 智能制造不是机器人
- OJ——对象数组输入输出
- CUDA学习