概述
Python内建了map()和reduce()函数
map()函数
原型:map(fn, lsd)
参数:
fn:是一个函数
reduce()函数
原型:reduce(fn, lsd)
参数: fn:一个函数 lsd:集合
功能:将传输的函数fn依次作用到lsd集合中的每个元素,并把结果作为一个Iterator返回
myList = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func</span><span class="hljs-params">(arg)</span>:</span>
<span class="hljs-keyword">return</span> arg**<span class="hljs-number">2</span>
res = map(my_func,myList)
print(list(res))
print(tuple(res))
print(set(res))
<span class="hljs-comment"># 使用lambda表达式来实现</span>
res = map(<span class="hljs-keyword">lambda</span> x:x**<span class="hljs-number">2</span>,myList)
print(list(res))
<span class="hljs-comment"># 使用内置数据类型转换函数实现</span>
res = map(int,[<span class="hljs-string">'1'</span>,<span class="hljs-string">'2'</span>,<span class="hljs-string">'3'</span>,<span class="hljs-string">'4'</span>,<span class="hljs-string">'5'</span>])
res = map(bool,[<span class="hljs-number">0</span>,<span class="hljs-string">'1'</span>,<span class="hljs-string">'2'</span>,<span class="hljs-string">'3'</span>,<span class="hljs-string">''</span>,<span class="hljs-string">'4'</span>,(),{},<span class="hljs-string">'5'</span>])
print(list(res))
reduce()函数 原型:reduce(fn, lsd) 参数: fn:一个函数 lsd:集合 功能:传入的fn函数作用在lsd集合中,这个fn函数必须接收两个参数,reduce把结果继续和序列中的下一个元素做累积运算
<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce
<span class="hljs-comment"># 进行值的累加</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span><span class="hljs-params">(x,y)</span>:</span>
print(x,y)
<span class="hljs-keyword">return</span> x+y
<span class="hljs-string">"""
1 2
3 3
6 4
10 5
15
"""</span>
my_list = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-comment"># res = reduce(add,my_list)</span>
<span class="hljs-comment"># 使用lambda表达式实现</span>
res = reduce(<span class="hljs-keyword">lambda</span> x,y:x+y,my_list)
print(res)
reduce&map组合使用案例
from functools import reduce
# 将字符串的 '12345' 变成整形的12345
print(reduce(lambda x,y:x*10+y,list(map(lambda x:int(x),list('12345')))))
"""
12345
1+2 1*10+2
3+3 12*10+3
6+4 123*10+4
10+5 1234*10+5 12345
"""
原型:filter(fn, lsd)
参数: fn:函数 lsd:集合 功能:用于过滤列表,把传入的fn函数依次作用在lsd集合中的每个元素上,然后根据返回True还是False决定是否保留该元素
li = [1,2,3,4,5,6,7,8,9,10]
# def func1(arr):
# for x in arr:
# if x % 2 == 0:
# arr.remove(x)
# func1(li)
def func2(num):
if num % 2 == 0:
return False
return True
res = filter(func2, li)
print(res)
print(list(res))
print(li)
删除列表中是空字符串的元素
li2 = ["a", "", "", "c", " ", " bsg "]
def f(item):
return item and item.strip()
res = filter(f, li2)
print(list(res))
排序算法
冒泡排序、快速排序、选择排序,计数器排序等
效率
在数据量小时冒泡和快速没有什么区别,但是当处理大量数据时快速排序的效率明显高于冒泡排序
冒泡算法原理
1 2 3 4 5 -> 5 4 3 2 1
1 2 3 4 5 -> 2 1 3 4 5 -> 2 3 1 4 5 -> 2 3 4 1 5 -> 2 3 4 5 1
0 1 1 2 2 3 3 4
2 3 4 5 1 -> 3 2 4 5 1 -> 3 4 2 5 1 -> 3 4 5 2 1
3 4 5 2 1 -> 4 3 5 2 1 -> 4 5 3 2 1
4 5 3 2 1 -> 5 4 3 2 1
li = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(len(li) - <span class="hljs-number">1</span>):
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(len(li) - i - <span class="hljs-number">1</span>):
<span class="hljs-keyword">if</span> li[j] < li[j+<span class="hljs-number">1</span>]:
temp = li[j]
li[j] = li[j+<span class="hljs-number">1</span>]
li[j+<span class="hljs-number">1</span>] = temp
print(li)
sorted()函数
原型:sorted(lsd, key=func)
参数: lsd:集合 key:规定排序的规则 功能:将led中的每个元素作用在key函数上,用函数的结果的大小来排序 优点:可以自定义排序规则
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(x)</span>:</span>
<span class="hljs-keyword">return</span> ord(x[<span class="hljs-number">2</span>])
li2 = [<span class="hljs-string">"awaefweg"</span>,<span class="hljs-string">"sgeweg3"</span>,<span class="hljs-string">"sgwrtwe"</span>,<span class="hljs-string">"wr2"</span>,<span class="hljs-string">"ergstrhrt"</span>]
<span class="hljs-comment"># li3 = sorted(li2, key=len, reverse=True)</span>
li3 = sorted(li2, key=func, reverse=<span class="hljs-keyword">True</span>)
print(li2)
print(li3)