#普通的条件语句
if 1 == 1:
name = "budongshu"
else:
name = "yangchi"
#三元表达式
name = "budongshu" if 1 == 1 else "yangchi"
#普通的函数
def func(arg):
return arg + 1
result = func(250)
print "result = ", result
#lambda表达式
my_lambda = lambda arg: arg + 1
print "result = ",my_lambda(250)
上面的方法,都实现了结果为 251 这个功能
>>> a = lambda x, y = 2: x + y
>>> a(1,3)
4
>>> b = lambda *z: z
>>> b(10,"budongshu")
(10, ‘budongshu‘)
>>> print map( lambda x: x + 3,range(6)) #map函数
[3, 4, 5, 6, 7, 8]
>>> print [x + 3 for x in range(6) ] #列表解析也可以做到
[3, 4, 5, 6, 7, 8]
那么我们什么时候使用map函数呢
原来,当序列多于一个时,map可以并行地对每个序列执行
>>> print map( lambda x,y: x*y,[1,2,3],[4,5,6]) #一对一的进行乘法运算 ,返回一个新的列表
[4, 10, 18]
>>> print map( lambda x,y: (x*y,x+y),[1,2,3],[4,5,6]) #同时运行加法和乘法 ,并把和,积 存在一个元组里,
[(4, 5), (10, 7), (18, 9)]
>>> print map(None,[1,2,3],[4,5,6]) #func为None的时候 ,把相同位置的元素存在一个元组里
[(1, 4), (2, 5), (3, 6)]
>>> li = [11,22,33]
>>> print reduce(lambda x,y:x + y,li)
66
>>> li = [11,22,33]
>>> print filter( lambda x: x>22 , li )
[33]
scores = [55,90,80,83,11,99.100,79] def score_filter( score): return score >= 80 and score < 90 #普通方法 filtered_score = [] for i in scores: if scores_filter(i): filtered_score,append(i) #filter()方法 print filter( score_filter,scores ) #列表解析 list comprehension方法 print [score for score in scores if score >=80 and score < 90]
一、迭代器(iterator)
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor)。
在Python中,for循环可以用于Python中的任何类型,包括列表、元祖等等,实际上,for循环可用于任何“可迭代对象”,
这其实就是迭代器,迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,
而在一系列结果的末尾是,则会引发StopIteration。任何这类的对象在Python中都可以用for循环或其他遍历工具迭代,
迭代工具内部会在每次迭代时调用next方法,并且捕捉StopIteration异常来确定何时离开。
>>> print range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print xrange(10)
xrange(10)
>>>
2 , 文件操作的 read 和 xreadlinex 的的区别
read会读取所有内容到内存
xreadlines则只有在循环迭代时才获取
二、生成器(constructor)
生成器函数在Python中与迭代器协议的概念联系在一起。简而言之,包含yield语句的函数会被特地编译成生成器。
当函数被调用时,他们返回一个生成器对象,这个对象支持迭代器接口。函数也许会有个return语句,但它的作用
是用来yield产生值的。不像一般的函数会生成值后退出,生成器函数在生成值后会自动挂起并暂停他们的执行和状态,
他的本地变量将保存状态信息,这些信息在函数恢复时将再度有效
>>> def g(n): ... for i in range(n): ... yield i ** 2 ... >>> for i in g(5): ... print i,":", ... 0 : 1 : 4 : 9 : 16 : >>> t = g(n) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ‘n‘ is not defined >>> t = g(5) >>> t.next() 0 >>> t.next() 1 >>> t.next() 4 >>> t.next() 9 >>> t.next() 16 >>> t.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
在运行完5次next之后,生成器抛出了一个StopIteration异常,迭代终止。
def new_range(num):
temp = -1
while True:
temp = temp + 1
if temp >= num:
return
else:
yield temp
y = new_range(10)
for i in y:
print i
需求:请按照从小到大对列表 [13, 22, 6, 99, 11] 进行排序
思路:相邻两个值进行比较,将较大的值放在右侧,依次比较!
分析: 每次循环的时候,都是用其中一个数跟其他的数进行比较 ,经过比较,找到此次循环最大的数,放在右侧,
但此列表中有五个数,需要依次比较出4个大的数放在右侧, 而每次寻找到一个最大数后,下次在进行比较的时候,
就可以忽略之前已经比较出来最大的数,所以就下面这个例子需要经过四个循环,每次循环的比较次数减掉一个 ,
最后每次最小的数就自动在最左侧了
#!/usr/bin/env python
li = [13, 22, 6, 99, 11]
for m in range(4):
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
for m in range(4):
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
for m in range(4):
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
for m in range(4):
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
经过分析代码如下:
#!/usr/bin/env python
#coding:utf-8
li = [13, 22, 6, 99, 11]
for i in range(1,len(li)): #循环四次
for m in range(len(li)-i): #每次循环次数减掉一个
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
利用函数编写如下数列:
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
def func(arg1,arg2):
if arg1 == 0:
print arg1, arg2
arg3 = arg1 + arg2
print arg3
func(arg2, arg3)
func(0,1)
原文:http://www.cnblogs.com/budongshu/p/a78f11be7a1c491a1958231566717b24.html