一.内置函数

1.sorted()排序函数

a:语法sorted(Iterable,key = None,reverse = False) Iterable:可迭代对象;key:排序规则(函数)

 lst = [5,7,6,12,1,13,9,18,5]
lst.sort() # sort是list里面的一个方法
print(lst) ll = sorted(lst, reverse=True) # 内置函数. 返回给你一个新列表 新列表是被排序的
print(ll)
#输出结果:
[1, 5, 5, 6, 7, 9, 12, 13, 18]
[18, 13, 12, 9, 7, 6, 5, 5, 1]

带有key函数的排序:

 lst = ["大阳哥a", "尼古拉斯aa", "赵四aaa", "刘能a", "广坤aaaaaa", "谢大脚a"]

 def func(s):
return s.count('a') # 返回数字 和下面lambda s:s.count('a')一样
 ll = sorted(lst, key=lambda s:s.count('a')) # 内部. 把可迭代对象中的每一个元素传递给func 
print(ll)
#输出结果
['大阳哥a', '刘能a', '谢大脚a', '尼古拉斯aa', '赵四aaa', '广坤aaaaaa']

2.lamda匿名函数

a:为了解决一些简单的需求而设计的一句话函数;lambda表示的是匿名函数,,不需要def来声明,一句话就可以;

语法:函数名 = lambda 参数:返回值

b:匿名函数统一都叫lambda,可以用__name__()查看函数名;

3.删选函数filter()

a:语法:filter(function,Iterable)在filter中会自动把iterable中的元素传递给function,然后根据function

返回True或者False来判断是否保留此项数据.

 lst = [1,2,3,4,5,6,7,8,9]
ll = filter(lambda i:i%2==1, lst)
# 第一个参数. 函数. 将第二个参数中的每一个元素传给函数. 函数如果返回True, 留下该元素.
print("__iter__" in dir(ll)) #返回的ll是一个迭代器
print("__next__" in dir(ll))
print(list(ll))
#输出结果:
True
True
[1, 3, 5, 7, 9]

4.map()映射函数

a:语法:map(function,iterable)可以对可迭代对象中的每一个元素进行映射,分别执行function;

 lst = [1,2,3,4,5,6,7,8,9,0,23,23,4,52,35,234,234,234,234,234,23,4]
it = map(lambda i: i * i, lst) # 把可迭代对象中的每一个元素传递给前面的函数进行处理. 处理的结果会返回成迭代器
print(list(it))
#输出结果
[1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 529, 529, 16, 2704, 1225, 54756, 54756, 54756, 54756, 54756, 529, 16]
 lst1 = [ 1, 2, 3, 4, 5]
lst2 = [ 2, 4, 6, 8]
print(list(map(lambda x, y:x+y, lst1, lst2))) # 如果函数中有多个参数. 后面对应的列表要一一对应
#输出结果
[3, 6, 9, 12]

二.递归

1.在函数中调用函数本身,就是递归

2.在Python中递归最大的深度是998;

3.可以用递归来遍历各种树形结构,比如文件夹系统,可以用递归来遍历该文件夹中的所有文件;

4.递归和while循环的区别,while循环,用的是同一个变量,只不过被重新赋值,而递归则是每次都开辟一个命名空间,重新调用;

5.关于文件夹的递归:

 import os
filepath = "d:\solor"
def read(filePath,n):
it = os.listdir(filePath) #查看文件中的文件,是一个可迭代对象
for line in it:
fp = os.path.join(filePath,line)
if os.path.isdir(fp): #判断文件是文件夹还是文件
print('\t'*n,line)
read(fp,n+1)
else:
print('\t'*n,line)
read(filepath,0)
#输出了文件夹列表

三.二分法三种模式

 lst = [11,22,33,44,55,66,77,88,99,123,234,345,456,567,678,789,1111]
n = 567
left = 0
right = len(lst) - 1
count = 1
while left <= right:
middle = (left + right) // 2
if n > lst[middle]:
left = middle + 1
elif n < lst[middle]:
right = middle - 1
else:
print(count)
print("存在")
print(middle)
break
count = count + 1
else:
print("不存在")
lst = [11,22,33,44,55,66,77,88,99,123,234,345,456,567,678,789,1111] def binary_search(left, right, n):
middle = (left + right)//2
if left > right:
return -1
if n > lst[middle]:
left = middle + 1
elif n < lst[middle]:
right = middle - 1
else:
return middle
return binary_search(left, right, n)
print(binary_search(0, len(lst)-1, 65) ) def binary_search(lst, n):
left = 0
right = len(lst) - 1
middle = (left + right) // 2
if right <= 0:
print("没找到")
return
if n > lst[middle]:
lst = lst[middle+1:]
elif n < lst[middle]:
lst = lst[:middle]
else:
print("找到了")
return
binary_search(lst, n)
binary_search(lst, 65)

python_012的更多相关文章

随机推荐

  1. 阿里云SLB产品HTTP、HTTPS、UDP协议使用

    1.http协议测试 第一步:添加http监听服务,前端端口为8080,后端端口为80,健康检查中检查端口为后端端口80: 第二步:在绑定的服务器上安装服务,步骤如下 centos系统中启动http协 ...

  2. LeetCode——160 Intersection of Two Linked Lists

    题目 Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: ...

  3. linux下安装nginx(nginx(nginx-1.8.0.tar.gz),openssl(openssl-fips-2.0.9.tar.gz) ,zlib(zlib-1.2.11.tar.gz),pcre(pcre-8.39.tar.gz))

    :要按顺序安装: 1:先检查是否安装 gcc ,没有先安装:通过yum install gcc-c++完成安 2:openssl : tar -zxf  openssl-fips-2.0.9.tar. ...

  4. [LeetCode] 834. Sum of Distances in Tree

    LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...

  5. 今天起,重新开头学习Java - 一、安装环境

    先拜领路人 https://blog.csdn.net/u011541946/article/category/6951961/3? 一.安装JDK 1. 下载 www.java.com JDK是Ja ...

  6. document.domain location.hostname location.host

    document.domain    location.hostname     location.host   :https://www.cnblogs.com/shd-study/p/103031 ...

  7. 分支结构 :if - else

    分支结构 :if - else 格式一: if(条件表达式){ 执行语句; } 格式二:二选一 if(条件表达式){ 执行语句1; }else{ 执行语句2; } 格式三: 多选一 if(条件表达式1 ...

  8. Python库指南

    Python库指南 1.time模块 作用:time模块是一个时间模块,与datetime模块它提供的功能是更加接近于操作系统层面. 应用场景:平时用的比较多的时间戳,等时间方面的操作,在爬虫方面经常 ...

  9. django 中 slice 和 truncatewords 不同用法???

    django中取一段字符串中的前 N 个字符,可以用 slice和truncatewords ,但是两者是有区别的. django的 模板过滤器 truncatewords ,取这个模板变量的前 N ...

  10. 构造函数与new的汇编实现

    this指针,通常是通过ecx传递:gcc是通过堆栈传递的,是最后一个被压栈.传递this指针是为了访问成员变量.除了虚函数,所有成员函数被编译之后都是全局函数.mov eax,[ecx] ; 将第一 ...