失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python拟合三元函数_python基础教程之常用内置函数 三元运算 递归

python拟合三元函数_python基础教程之常用内置函数 三元运算 递归

时间:2021-01-08 09:38:12

相关推荐

python拟合三元函数_python基础教程之常用内置函数 三元运算 递归

目录

常用内置函数

abs/round/sum

eval/exec

enumerate

max/min

sorted

zip

map

filter

补充:reduce

lambda

初识递归

再谈递归

递归函数与二分查找算法

回到顶部

常用内置函数

Built-in Functions

abs()

dict()

help()

min()

setattr()

all()

dir()

hex()

next()

slice()

any()

divmod()

id()

object()

sorted()

ascii()

enumerate()

input()

oct()

staticmethod()

bin()

eval()

int()

open()

str()

bool()

exec()

isinstance()

ord()

sum()

bytearray()

filter()

issubclass()

pow()

super()

bytes()

float()

iter()

print()

tuple()

callable()

format()

len()

property()

type()

chr()

frozenset()

list()

range()

vars()

classmethod()

getattr()

locals()

repr()

zip()

compile()

globals()

map()

reversed()

__import__()

complex()

hasattr()

max()

round()

delattr()

hash()

memoryview()

set()

回到顶部

abs/round/sum

>>> abs(1)>>> abs(-1) #求绝对值

>>> round(1.234,2)1.23

>>> round(1.269,2) #四舍五入

1.27

>>> sum([1,2,3,4])>>> sum((1,3,5,7)) #接收数字组成的元组/列表

>>> def func():pass

>>> callable(func) #判断一个变量是否可以调用 函数可以被调用

True>>> a = 123 #数字类型a不能被调用

>>>callable(a)

False>>> chr(97) #将一个数字转换成一个字母

'a'

>>> chr(65)'A'

>>> dir(123) #查看数字类型中含有哪些方法

['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']>>> dir('abc')

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

回到顶部

eval/exec

>>> eval('1+2-3*4/5') #执行字符串数据类型的代码并且将值返回

0.6000000000000001

>>> exec('print(123)') #执行字符串数据类型的代码但没有返回值

123

回到顶部

enumerate

>>> enumerate(['apple','banana'],1) #会给列表中的每一个元素拼接一个序号

>>> list(enumerate(['apple','banana'],1))

[(1, 'apple'), (2, 'banana')]

回到顶部

max/min

>>> max(1,2,3,) #求最小值

3

>>> min(2,1,3) #求最大值

1

回到顶部

sorted

将给定的可迭代对象进行排序,并生成一个有序的可迭代对象。

>>> sorted([1, 4, 5, 12, 45, 67]) #排序,并生成一个新的有序列表

[1, 4, 5, 12, 45, 67]

还接受一个key参数和reverse参数。

>>> sorted([1, 4, 5, 12, 45, 67], reverse=True)

[67, 45, 12, 5, 4, 1]

list1 =[

{'name': 'Zoe', 'age': 30},

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

{'name': 'Tom', 'age': 22},

{'name': 'Jack', 'age': 40},

]

ret= sorted(list1, key=lambda x: x['age'])print(ret)#[{'name': 'Bob', 'age': 18}, {'name': 'Tom', 'age': 22}, {'name': 'Zoe', 'age': 30}, {'name': 'Jack', 'age': 40}]

回到顶部

zip

zip函数接收一个或多个可迭代对象作为参数,最后返回一个迭代器:

>>> x = ["a", "b", "c"]>>> y = [1, 2, 3]>>> a = list(zip(x, y)) #合包

>>>a

[('a', 1), ('b', 2), ('c', 3)]>>> b =list(zip(*a)) #解包

>>>b

[('a', 'b', 'c'), (1, 2, 3)]

zip(x, y) 会生成一个可返回元组 (m, n) 的迭代器,其中m来自x,n来自y。 一旦其中某个序列迭代结束,迭代就宣告结束。 因此迭代长度跟参数中最短的那个序列长度一致。

>>> x = [1, 3, 5, 7, 9]>>> y = [2, 4, 6, 8]>>> for m, n inzip(x, y):

...print(m, n)

...2

4

6

8

如果上面不是你想要的效果,那么你还可以使用 itertools.zip_longest() 函数来代替这个例子中的zip。

>>> from itertools importzip_longest>>> x = [1, 3, 5, 7, 9]>>> y = [2, 4, 6, 8]>>> for m, n inzip_longest(x, y):

...print(m, n)

...2

4

6

8None

zip其他常见应用:

>>> keys = ["name", "age", "salary"]>>> values = ["Andy", 18, 50]>>> d =dict(zip(keys, values))>>>d

{'name': 'Andy', 'age': 18, 'salary': 50}

回到顶部

map

map()接收两个参数func(函数)和seq(序列,例如list)。如下图:

map()将函数func应用于序列seq中的所有元素。在Python3之前,map()返回一个列表,列表中的每个元素都是将列表或元组“seq”中的相应元素传入函数func返回的结果。Python 3中map()返回一个迭代器。

因为map()需要一个函数作为参数,所以可以搭配lambda表达式很方便的实现各种需求。

例子1:将一个列表里面的每个数字都加100:

如果觉得《python拟合三元函数_python基础教程之常用内置函数 三元运算 递归》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。