函数

函数参数的传递

def test_fun(a,b=6,*c,**d):#c为元组,d为字典
    print("a=",a,",b=",b,",c=",c,",d=",d)
test_fun(1)
####
a= 1 ,b= 6 ,c= () ,d= {}
>>>test_fun(1,2)
a= 1 ,b= 2 ,c= () ,d= {}
>>>test_fun(1,2,3)
a= 1 ,b= 2 ,c= (3,) ,d= {}
>>>test_fun(1,2,3,4)
a= 1 ,b= 2 ,c= (3, 4) ,d= {}
>>>test_fun(a=1,b=2,c=3)
a= 1 ,b= 2 ,c= () ,d= {'c': 3}
>>>test_fun(1,2,3,4,b=1)#对b进行了二次赋值,不能与前面参数重名
TypeError    
>>>test_fun(1,2,3,4,x=1)
a= 1 ,b= 2 ,c= (3, 4) ,d= {'x': 1}

函数文档

def 函数名():
‘’‘注释’’’
函数体
print()

def fun_test():
    '''This is a function,it has no program'''
    print("Hi!")
>>>help(fun_test)
Help on function fun_test in module __main__:
fun_test()
    This is a function,it has no program
>>>fun_test.__doc__
'This is a function,it has no program'

lambda匿名函数

<函数名> = lambda <参数>:<表达式>
等价于
def <函数名>(<参数>):
<函数体>
return <返回值>

  • lambda是一种匿名函数,即没有名字的函数
  • 使用lambda保留字定义,函数名是返回结果
  • lambda用于定义简单的、能够在一行内表示的函数
  • 不允许包含复合语句,一定是简单句
  • 可以用lambda来定义有名字的函数
>>>f = lambda x:x+5
>>>f(3)
8
>>>sorted(['abc','afe','acb'],key=lambda x:(x[0],x[2]))
#第一次x='abc'所以(x[0],x[2])=('a','c')
#第二个('a','e')
#第三个('a','b')
#最后进行排序
['acb', 'abc', 'afe']

range函数

range(start,stop,step)
  • start:默认从0开始,如range(5)==(0,5)
  • stop:不包含最后的数,如range(0,5)–>(0,1,2,3,4)
  • step:默认步长为1
>>>list(range(5))
[0,1,2,3,4]
>>>list(range(0))
[]
>>>list(range(1,0))
[]
>>>list(range(0,30,5))
[0,5,10,15,20,25]

map函数

map(function,iterable, ...)

返回包含每次function函数返回值的新列表

def square(x):#计算平方数
    return x**2
map (square,[1,2,3,4,5])#计算列表各个元素的平方数
####返回###
<map at 0x21fa2069748>
>>>map(lambda x:x**2,[1,2,3,4,5])#输出迭代器
<map at 0x21fa20699e8>
>>>for i in map(lambda x,y:x+y,[1,2,3,4],[2,3,4,5]):
    print(i**2,end='\t')

9	25	49	81
>>>def add(x,y,z):
    return x+y+z
>>>list1=[1,2,3]
>>>list2=[1,2,3,4]
>>>list3=[1,2,3,4,5]
>>>res=map(add,list1,list2,list3)
>>>print(list(res))             #按照最短的来计算,截断
[3,6,9]
>>>list(map(str,[1,2,3]))
['1', '2', '3']
>>>list(map(lambda x:len(x),['a','bb','ccc']))
[1, 2, 3]
>>>x,y,z=map(str,range(3))
>>>y
'1'

filter函数

  • 过滤器,过滤掉不符合条件的元素,只能接受一个参数,返回True或False,最后返回True的元素放到新列表中
  • 生成迭代器,返回迭代器对象
filter(function, iterable)
def is_odd(n):
    return n%2 == 1
newlist=filter(is_odd,[1,2,3,4,5,6,7,8,9,10])
print(newlist)
------------
<filter object at 0x0000021FA20F04A8>
import math
def is_sqr(x):
    return math.sqrt(x) % 1 == 0
 
newlist = filter(is_sqr, range(1, 101))
print(list(newlist))
------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>list(filter(lambda x:len(x)>3,['a','b','abcd']))
['abcd']

zip函数

函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

zip([iterable, ...])
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b)     # 打包为元组的列表
>>>list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
>>>list(zip(a,c))        # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>>list(zip(*zipped))       # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
>>>c = dict(zip(a,b)) #使输出为字典形式
>>>c
{1: 4, 2: 5, 3: 6}
>>>dict(zip([1,2],[3,4]))
{1: 3, 2: 4}
>>>x='abcd'
>>>y='abcde'
>>>[i==j for i,j in zip(x,y)]
[True, True, True, True]

enumerate函数

  • 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
    enumerate(sequence, [start=0])

  • sequence – 一个序列、迭代器或其他支持迭代对象。

  • start – 下标起始位置。

  • 返回 enumerate(枚举) 对象。

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>>list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>>list(enumerate(seasons, start=1))      # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
seq = ['one','two','three']
for i, element in enumerate(seq):
    print (i,element)
----------
0 one
1 two
2 three
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值