python nums函数_python 函数2

nums = [1,2,3,4,5]

map函数

map(函数,序列)

map 是把数组中的值一个一个的进行某种处理,把处理后的值放到一个新的数组中,并返回这个新的数组。

map(lambda x:x++2,[1,2,3,4])

返回的是可迭代对象

for i in map(lambda x:x++2,[1,2,3,4]):

print(i)

3

4

5

6

def test(x):

if(x>=1):

x = x - 1

return x+test(x)

else:

return 0

for i in map(test,[1,2,3,4]):

print(i)

0

1

3

6

>>> list(map(test,[1,2,3,4]))

[0, 1, 3, 6]

filter函数

filter是通过某种筛选条件把数组中符合条件的值放到一个新的数组中,并返回这个新数组

filter(函数,序列)

拿出布尔值

>>> list(filter(lambda x:x.startswith("a"),["aa","bb","abc","abd"]))

['aa', 'abc', 'abd']

def test1(x):

if(len(x) > 2):

return True

else:

return False

>>> list(filter(test1,["aa","bb","abc","abd"]))

['abc', 'abd']

reduce函数

reduce处理一个序列,把序列合并操作

>>> def add(x, y):

return x+y

reduce(add, [1,2,3,4])

10

abs() 绝对值

all() 拿出值做布尔预算

bool 转化为bool值

bytes("aa',encoding="utf-8").decode("utf-8")

>>> bytes("选项",encoding="utf-8")

b'\xe9\x80\x89\xe9\xa1\xb9'

bytes(b'\xe9\x80\x89\xe9\xa1\xb9').decode("utf-8")

'选项'

chr() ascii码

dir() 查看

divmod(10,3)

(3,1)  取商取余

eval() 函数

简单表达式

print(eval('1+2'))

3

字符串转字典

print(eval("{'name':'linux','age':18}")

{'name':'linux','age':18}

全局变量

globals()

局部变量

locals()

max() 最大值

min() 最小值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值