lambda表达式pythonlist_Lambda表达式以及python的内置函数

2ff34e647e2e3cdfd8dca593e17d9b0a.png

lambda.jpg

Lambda表达式1

2

3

4

5

6

7

8temp= lambda x,y:x+y

print temp(4,5)

temp= lambda x,y,z:x*y*z

print temp(4,5,2)

list(map(lambda x:x*2,range(10)))

注意: Lambda函数是匿名函数,一般结合map

内置函数1

2

3

4

5

6

7

8a=[]

print dir() \列出来的是key

print vars() \列出来的是key - value

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a']

{'a': [], '__builtins__': , '__file__': 'D:/distribute_project_py/shop.py', '__package__': None, '__name__': '__main__', '__doc__': 'nproduct = {nn 'xe8x8bxb9xe6x9ex9cxe6x89x8bxe6x9cxba':3000,n 'mackair':8000,n 'xe7x89x99xe5x88xb7':8,n 'xe5x85x85xe7x94xb5xe7xbaxbf':80,n 'xe9x93x85xe7xacx94':15nn}nmoney =raw_input("xe8xafxb7xe5x85x85xe5x80xbcxe9x87x91xe9xa2x9d:")nprint 'xe5x85x85xe5x80xbcxe6x88x90xe5x8ax9fxe9x87x91xe9xa2x9dxe4xb8xba %s' %moneynshop=[]nprint "-"*12 +u"xe5x95x86xe5x93x81xe5x88x97xe8xa1xa8" +"-"*12nfor i in product:nn print 'xe5x90x8dxe7xa7xb0:%s---->xe4xbbxb7xe6xa0xbcxefxbcx9a%d'%(i,product[i])nprint "-"*12 +u"xe5x95x86xe5x93x81xe5x88x97xe8xa1xa8" +"-"*12nsheng_money=moneynwhile True:n buy=raw_input(u"xe4xbdxa0xe9x9cx80xe8xa6x81xe8xb4xadxe7x89xa9xe5x90x97xefxbcx9fy/n")n if buy=='y':n salry=raw_input(u"xe8xbex93xe5x85xa5xe4xbdxa0xe8xa6x81xe8xb4xadxe4xb9xb0xe7x9ax84xe5x95x86xe5x93x81:")n if salry in product:n temp=int(sheng_money)-int(product[salry])n sheng_money=tempn if sheng_money>=0:n print 'xe6x81xadxe5x96x9cxe4xbdxa0xe8xb4xadxe4xb9xb0xe6x88x90xe5x8ax9f,xe8xb4xadxe4xb9xb0xe7x9ax84xe5x95x86xe5x93x81xefxbcx9a%s ,xe5x89xa9xe4xbdx99xe9x87x91xe9xa2x9dxe4xb8xbaxefxbcx9a%d,xe5x95x86xe5x93x81xe4xbbxb7xe6xa0xbcxe4xb8xbaxefxbcx9a%d' %(salry,sheng_money,product[salry])n else:n print 'xe5xbdx93xe5x89x8dxe5x89xa9xe4xbdx99xe9x87x91xe9xa2x9dxefxbcx9a%d,xe5x95x86xe5x93x81xe4xbbxb7xe6xa0xbcxefxbcx9a%d,xe5x89xa9xe4xbdx99xe9x87x91xe9xa2x9dxe4xb8x8dxe8xb6xb3xe8xafxb7xe5x85x85xe5x80xbcxefxbcx81' %(sheng_money,product[salry])n breakn else:n print 'xe6xb2xa1xe6x9cx89xe8xafxa5xe5x95x86xe5x93x81%s' %salryn salry=raw_input(u"xe8xafxb7xe9x87x8dxe6x96xb0xe8xbex93xe5x85xa5:")n else:n print "xe6xacxa2xe8xbfx8exe4xb8x8bxe6xacxa1xe5x85x89xe4xb8xb4xefxbcx81"n breakn '}

help(vars())1

2

3type(a)

查看一个变量的类型

导入模块1

2from file import demo

reload(demo)

id(),内存地址1

2

3

4

5

6

7t1 = 123

t2=999

print id(t1)

print id(t2)

6793536 //t1的内存地址

44305828 //t2的内存地址

Abs(),取绝对值1

2

3print abs(-9)

9

Bool(),判断真假1

2

3

4print bool(1) //输出True

print bool(0) //输出False

print bool(15) //输出True

print bool(-1) //输出True

divmod(),数字处理函数,实用场景例如:分页1

2

3

4

5print divmod(9,4)

print divmod(9,3)

(2, 1)

(3, 0)

cmp( x, y),比较大小,如果x>y,则返回1,负责返回-11

2

3

4print cmp(4,8)

-1

print cmp(4,2)

1

Max(),返回参数的最大值1

2

3print max([11,33,55,22,9,77,38])

77

min(),返回参数的最小值1

2

3print min([11,33,55,22,9,77,38])

9

sum(iterable[, start]),用于计算可迭代对iterable的和1

2

3print sum([11,33,55,22,9,77,38])

245

Pow(x,y),用于求参数x的多少次方y1

2

3print pow(2,10)

1024

Len(),返回字符串的长度,如果是中文,则返回的是字节的长度1

2

3

4

5

6

7print len('alexzhou')

8

print len('你好')

6

all(iterable),判断可迭代的值里面如果有出现的值为0,则返回的值为false,否则返回True1

2

3

4

5

6print all([1,2,3,4,7])

print all([1,2,3,4,0,1,22])

True

False

Any(iterable), 判断可迭代的值里面如果值都为0,则返回的值为false,否则返回True1

2

3

4

5

6

7print any([1,2,3,4,7])

print any([1,2,3,4,0,1,22])

print any([0,0,0,0,0])

True

True

False

Chr(),根据参数值ascii,返回对应的字符串1

2

3

4

5

6

7

8

9

10

11

12print chr(65)

print chr(44)

print chr(66)

print chr(67)

print chr(68)

A

,

B

C

D

Ord(),根据参数值,返回对应的ascii的转换(实用于验证码数字字母转换)1

2

3print ord('A')

print ord('B')

print ord('C')

hex()、bin()、oct(),八进制,16进制,2进制之间的转换1

2

3

4

5

6

7print hex(20)

print bin(20)

print oct(20)

0x14

0b10100

024

Enumerate(),添加索引序号,返回值可以根据自定义的序号返回参数1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16shop=['猕猴桃','香蕉','橙子']

for i in shop:

print i

for team in enumerate(shop,10):

print team[0],team[1]

猕猴桃

香蕉

橙子

10 猕猴桃

11 香蕉

12 橙子

format函数格式化字符串,{0},{1}相当于占位符%s1

2

3s='我 叫 {0},{1}'

print s.format('Alexzhou','xx')

apply(),用于执行函数1

2

3

4def say(a,b):

print a,b

apply(say,("hello","Alex"))

map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回,常常于lambda一起使用1

2

3

4

5

6print map(lambda x:(x*2)-1,range(1,50))

print map(lambda x:x*2,range(51))

100以内的奇偶数

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97]

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]

Filter(),用于过滤条件,过滤1

2

3

4

5

6

7

8

9

10shop=[11,22,33]

def foo(arg):

if arg<22:

return True

else:

return False

temp=filter(foo,shop)

print temp

[11]

Reduce(),累加,累乘…,必须要接收两个参数1

2

3

4shop=[11,22,33]

print reduce(lambda x,y:x+y,shop)

zip(),拼接列表的列组成一个新的序列1

2

3

4

5x=[1,2,4]

y=[4,5]

z=[4,5,6]

t =[8,9,10]

print zip(x,y,z,t)

eval(),字符串当做表达式执行1

2a= '9+9'

print eval(a)

反射

通过字符串的形式去导入模块,并以字符串的形式执行函数1

2

3temp = 'mysqlheerp'

mode = __import__(temp)

mode.cout()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值