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
本文深入探讨了Python编程中的关键概念和技术,包括缩进规则、条件表达式、pass语句、迭代器、列表解析和生成器的应用。文章还介绍了如何高效处理文件、计算文件字符数、实现输入和总和计算,以及利用生成器节省内存的方法。
1036

被折叠的 条评论
为什么被折叠?



