补充几个高级函数

zip

  • 把两个可迭代内容生成一个可迭代的tuple元素类型组成的内容
# zip 案例
l1 = [ 1,2,3,4,5]
l2 = [11,22,33,44,55]z = zip(l1, l2)print(type(z))
print(z)for i in z:print(i)help(zip)
<class 'zip'>
<zip object at 0x054A1BE8>
(1, 11)
(2, 22)
(3, 33)
(4, 44)
(5, 55)
Help on class zip in module builtins:class zip(object)|  zip(iter1 [,iter2 [...]]) --> zip object|  |  Return a zip object whose .__next__() method returns a tuple where|  the i-th element comes from the i-th iterable argument.  The .__next__()|  method continues until the shortest iterable in the argument sequence|  is exhausted and then it raises StopIteration.|  |  Methods defined here:|  |  __getattribute__(self, name, /)|      Return getattr(self, name).|  |  __iter__(self, /)|      Implement iter(self).|  |  __next__(self, /)|      Implement next(self).|  |  __reduce__(...)|      Return state information for pickling.|  |  ----------------------------------------------------------------------|  Static methods defined here:|  |  __new__(*args, **kwargs) from builtins.type|      Create and return a new object.  See help(type) for accurate signature.
l1 = ["wangwang", "mingyue", "yyt"]
l2 = [89, 23, 78]z = zip(l1, l2)for i in z:print(i)# 考虑下面结果,为什么会为空
l3 = [i for i in z]
print(l3)
('wangwang', 89)
('mingyue', 23)
('yyt', 78)
[]

enumerate

  • 跟zip功能比较像
  • 对可迭代对象里的每一元素,配上一个索引,然后索引和内容构成tuple类型
# enumerate案例1
l1 = [11,22,33,44,55]em = enumerate(l1)l2 = [i for i in em]
print(l2)
[(0, 11), (1, 22), (2, 33), (3, 44), (4, 55)]
em = enumerate(l1, start=100)l2 = [ i for i in em]
print(l2)
[(100, 11), (101, 22), (102, 33), (103, 44), (104, 55)]

collections模块

  • namedtuple
  • deque

namedtuple

  • tuple类型
  • 是一个可命名的tuple
import collections
Point = collections.namedtuple("Point", ['x', 'y'])
p = Point(11, 22)
print(p.x)
print(p[0])
11
11
Circle = collections.namedtuple("Circle", ['x', 'y', 'r'])c = Circle(100, 150, 50)
print(c)
print(type(c))# 想检测以下namedtuple到底属于谁的子类
isinstance(c, tuple)
Circle(x=100, y=150, r=50)
<class '__main__.Circle'>True

dequeue

  • 比较方便的解决了频繁删除插入带来的效率问题
from collections import dequeq = deque(['a', 'b', 'c'])
print(q)q.append("d")
print(q)q.appendleft('x')
print(q)
deque(['a', 'b', 'c'])
deque(['a', 'b', 'c', 'd'])
deque(['x', 'a', 'b', 'c', 'd'])

defaultdict

  • 当直接读取dict不存在的属性时,直接返回默认值
d1 = {"one":1, "two":2, "three":3}
print(d1['one'])
print(d1['four'])
1---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)<ipython-input-27-d54a61646604> in <module>()1 d1 = {"one":1, "two":2, "three":3}2 print(d1['one'])
----> 3 print(d1['four'])KeyError: 'four'
from collections import defaultdict
# lambda表达式,直接返回字符串
func = lambda: "saedade"
d2 = defaultdict(func)d2["one"] = 1
d2["two"] = 2print(d2['one'])
print(d2['four'])
1
saedade

Counter

  • 统计字符串个数
from collections import Counter# 为什么下面结果不把abcdefgabced.....作为键值,而是以其中每一个字母作为键值
# 需要括号里内容为可迭代
c = Counter("abcdefgabcdeabcdabcaba")
print(c)
Counter({'a': 6, 'b': 5, 'c': 4, 'd': 3, 'e': 2, 'f': 1, 'g': 1})
s = ["sadfa", "love", "love", "love", "love", "asdfaed"]
c = Counter(s)print(c)
Counter({'love': 4, 'sadfa': 1, 'asdfaed': 1})

python 高级函数补充相关推荐

  1. python counter函数定义_分享几个自己常用的Python高级函数

    哈喽大家好我是蚂蚁,今天给大家分享几个我自己常用的Python相对高级点的函数,这些函数在特定的场景下能节省大量的代码. 简单列举一下我想要介绍的几个函数: counter:计数器 defaultdi ...

  2. Python高级函数Counter、defaultdict、map、reduce、filter使用

    在这里为大家介绍一下Python非常实用的Counter.defaultdict.map.reduce.filter的函数使用,提高大家在平时使用Python的效率 计数器函数 Counter 带默认 ...

  3. python高级函数,将函数作为变量、返回函数

    python中使用函数作为参数 在python中,我们可以用一个变量来存放函数.示例: a = len length = a([1,2,3,4,5]) print(length) 在上面我将len() ...

  4. python高级函数、将函数作为变量、返回函数_从函数外部返回变量名,作为python函数内部的字符串...

    因此,我创建了一个函数,它将一个操作(在本例中,一个数组与一个正弦波进行逐点乘法,但这与我的问题无关).在 现在我已经创建了另一个函数,我想用它创建一个string的python代码,以便以后多次应用 ...

  5. python高级函数六剑客

    第一位:lambda 1.lambda语句被用来创建新的函数对象,并且在运行时返回它们. 2.Python使用lambda关键字来创建匿名函数.这种函数得名于省略了用 def声明函数的标准步骤. 3. ...

  6. Python高级函数

    1. filter(func, lterable) 函数作用于序列, 返回True保留该元素2. map(func, lterable) 将传入的函数依次作用到序列的每个元素, 并返回新的列表(惰性序 ...

  7. Python高级函数--map/reduce

    名字开头大写 后面小写:练习: 1 def normalize(name): 2 return name[0].upper() + name[1:].lower() 3 L1 = ['adam', ' ...

  8. Python学习之旅三:python高级语法

    使用pycharm和jupter notebook. 1 包 1.1 模块 一个模块就是一个包含python代码的文件,后缀名为.py即可,模块就是个python文件. 1.1.1 为什么要使用模块呢 ...

  9. python函数+定义+调用+多返回值+匿名函数+lambda+高级函数(reduce、map、filter)

    python函数+定义+调用+多返回值+匿名函数+lambda+高级函数(reduce.map.filter) Python 中函数的应用非常广泛,比如 input() .print().range( ...

  10. python 元类的call_python3 全栈开发 - 内置函数补充, 反射, 元类,__str__,__del__,exec,type,__call__方法...

    python3 全栈开发 - 内置函数补充, 反射, 元类,__str__,__del__,exec,type,__call__方法 一, 内置函数补充 1,isinstance(obj,cls)检查 ...

最新文章

  1. Java项目:学生学科竞赛管理管理系统设计和实现(java+springboot+ssm+maven)
  2. Java必刷100题
  3. java怎么快速补缺_Java查漏补缺-小细节
  4. Java 洛谷 P1059 [NOIP2006 普及组] 明明的随机数
  5. 阿里大神的刷题笔记.pdf
  6. 操作系统中的内存分配
  7. Redis Cluster集群模式
  8. js跟php增加删除信息,浅谈JavaScript数组的添加和删除
  9. Design Patterns(设计模式-发布/订阅)
  10. flutter图片识别_从头到尾撸一遍Flutter的一切...
  11. r语言提取列名_玩转数据处理120题之P1-P20(R语言tidyverse版本)
  12. 有奖征文 | 蒋涛邀你悦评《UNIX传奇》新书,赢技术进阶好礼
  13. 一个医院内的计算机网络系统属于,医院信息管理系统
  14. 【修电脑】电脑将在1分钟后重启
  15. 我的世界服务器java出错_如何看懂 游戏《Minecraft》的错误报告 客户端/服务端...
  16. Android内存优化大全(二)
  17. IDF实验室之万里寻踪红与黑
  18. 可用的公开 RTSP/ RTMP 在线视频流资源地址(亲测可行)
  19. 无耻,无知的韩国人!
  20. Oracle中如何计算时间差

热门文章

  1. Java使用PDFBox将一个 PDF 文档拆分为多个 PDF
  2. java实现将PDF文件拆分成图片
  3. 互联网大佬“打脸”简史:马云/雷军/罗永浩/刘强东...
  4. SAP 财务月结之 外币评估(TCODE:FAGL_FC_VAL,S4版本用 FAGL_FCV)<转载>
  5. kaggle初探--泰坦尼克号生存预测
  6. 非常全面的IReport的使用
  7. Java swing+Mysql商品销售管理系统
  8. elf 变异upx 脱壳
  9. thinkpadt410接口介绍_thinkpadt410价格与评测介绍【图文】
  10. ubuntu下flv 批量转化成 mp3格式脚本