sorted 、 filter 、 map
sorted 排序函数
内置函数中提供了一个通用的排序方案 ,返回一个新的列表,不会改变原数据
语法: sorted(iterable, key, reverse)
key: 排序方案, sorted函数内部会把可迭代对象中的每一个元素拿出来交给后面的key
后面的key计算出一个数字. 作为当前这个元素的权重, 整个函数根据权重进行排序
reverse 参数默认为False(升序)
lst = [
{'name':"汪峰","age":48},
{"name":"章子怡",'age':38},
{"name":"alex","age":39},
{"name":"wusir","age":32},
{"name":"赵一宁","age":28}
] # 根据年龄排序
ll = sorted(lst, key=lambda le: le['age'])
print(ll) # 根据名字长度排序
ll = sorted(lst, key=lambda le: len(le['name']))
print(ll) filter 过滤函数
filter(function, iterable) 返回一个迭代器 可通过for list() next() 取值
把可迭代对象中的每一个元素交给前面的函数进行筛选. 函数返回True或者False
#留下大于60分的
lst = [
{"name":"汪峰", "score":48},
{"name":"章子怡", "score":39},
{"name":"赵一宁","score":97},
{"name":"石可心","score":90}
] ll = filter(lambda el: el['score'] > 60, lst)
print(ll) #==》<filter object at 0x02CA08B0> 迭代器地址
print(list(ll)) #==》[{'name': '赵一宁', 'score': 97}, {'name': '石可心', 'score': 90}]
map 映射函数
语法 map(function, iterable) 返回一个迭代器于filter()类似
把可迭代对象中的数据交给前面的函数进行执行. 返回值就是map的处理结果
# 返回一个可迭代数据的平方
lst = [1, 2, 3]
ll = map(lambda n : n**2, lst)
# print(list(ll))
for el in ll:
print(el)
map可以将可迭代对象进行处理,与zip()类似有水桶效应,多出的项不做处理
# 水桶效应, zip()
lst1 = [1, 3, 5, 7]
lst2 = [2, 4, 6, 8, 10]
m = map(lambda x, y, z: x + y + z, lst1, lst2, [5, 1, 2, 3, 6])
print(list(m))
输出:[8, 8, 13, 18]
sorted 、 filter 、 map的更多相关文章
- Python高阶函数map、reduce、filter、sorted的应用
#-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.wait import Web ...
- python中的map、reduce、filter、sorted函数
map.reduce.filter.sorted函数,这些函数都支持函数作为参数. map函数 map() 函数语法:map(function, iterable, ...) function -- ...
- react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- Python基础三. 函数、lambda、filter、map、reduce
一.概述 函数, 就是用一些语句组织起来实现一组特定的功能, 用来重复调用. 函数的作用及意义:最大化的重用代码和最小化的代码冗余以及对流程的分解. Python中有哪些函数: 内建的函数 第三方模块 ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- Python 之map、filter、reduce
MAP 1.Python中的map().filter().reduce() 这三个是应用于序列的内置函数,这个序列包括list.tuple.str. 格式: 1>map(func,swq1[,s ...
- python-lambda、filter、reduce、map
python-lambda.map.filter.reduce lamdba python关键字,用于在表达式中创建匿名函数. 注意:lambda函数的定义体只能用纯表达式,不能赋值,不能使用whil ...
- ES5新增数组方法every()、some()、filter()、map()
JavaScript ES5标准中新增了一些Array方法,如every().some().filter().map().它们的出现使我们能够更加便利地操作数组,但对IE9以下浏览器的兼容性比较差.下 ...
- JavaScript高级编程——Array数组迭代(every()、filter()、foreach()、map()、some(),归并(reduce() 和reduceRight() ))
JavaScript高级编程——Array数组迭代(every().filter().foreach().map().some(),归并(reduce() 和reduceRight() )) < ...
随机推荐
- shell实现洗牌随机
洗牌问题: 洗一副扑克,有什么好办法?既能洗得均匀,又能洗得快?即相对于一个文件来说怎样 高效率的实现乱序排列? 关于洗牌问题,其实已经有了一个很好的shell解法,这里另外给三个基于AWK的方法, ...
- es6中顶层对象属性≠全局属性
先思考一下下面代码的输出结果是什么 const a = { x:1, fn:()=>this.x+=1 } const x = 1 a.fn() console.log(a.x,x) 正确答案为 ...
- ORA-06519: 检测到活动的自治事务处理,已经回退
写了一个函数,由于在定义时加入了 create or replace function F_计算结果(In_参数 varchar2) return number is --使用自治事务PRAGMA A ...
- 访问JavaBean
<jsp:useBean> 标签可以在JSP中声明一个JavaBean,然后使用.声明后,JavaBean对象就成了脚本变量,可以通过脚本元素或其他自定义标签来访问.<jsp:use ...
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- WPF 后台任务 等待动画 样例 && C# BackgroundWorker 详解
运行效果: 前台代码: <Window x :Class="Waiting.Window1" xmlns="http://schemas.microsoft.com ...
- 检测手机中是否安装了google地图,没有则提示安装,并跳转到地图查找特定的地点
/** * 检测手机中是否安装了某个特定的app,若没有提示安装 */ PackageInfo name_2 = null; try { // 若没有这个包名会异常 name_2 = getPacka ...
- python爬虫:正则表达式
符号: . : 匹配任意字符(类似占位符,多少个.就表示多少个字符),换行符除外(与re.S相反) *:匹配前面一个字符0次或无限次 ?:匹配前面一个字符0次或1次 组合: .* : 贪心算法 一次匹 ...
- ApiDoc 和 Swagger 接口文档
ApiDoc:https://blog.csdn.net/weixin_38682852/article/details/78812244 Swagger git: https://github.co ...
- linux以下C 利用openssl的AES库加密,解密
OpenSSL提供了AES加解密算法的API const char *AES_options(void); AES算法状态,是所有支持或者是部分支持. 返回值:"aes(full)" ...