python 高级函数补充

举报
ruochen 发表于 2021/03/28 03:11:25 2021/03/28
【摘要】 高级函数补充 zip 把两个可迭代内容生成一个可迭代的tuple元素类型组成的内容 # zip 案例 l1 = [1,2,3,4,5] l2 = [11,22,33,44,55] z = zip(l1, l2) print(type(z)) for i in z: print(i) 12345678910 <class 'zip'> (1, 1...

高级函数补充

zip

  • 把两个可迭代内容生成一个可迭代的tuple元素类型组成的内容
# zip 案例
l1 = [1,2,3,4,5]
l2 = [11,22,33,44,55]

z = zip(l1, l2)

print(type(z))

for i in z: print(i)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<class 'zip'>
(1, 11)
(2, 22)
(3, 33)
(4, 44)
(5, 55)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
('wangwang', 89)
('mingyue', 23)
('yyt', 78)
<generator object <genexpr> at 0x000001ED9F1F3ED0>

  
 
  • 1
  • 2
  • 3
  • 4

enumerate

  • 跟zip功能比较像
  • 对可迭代对象里的每一元素,配上一个索引,然后索引和内容构成tuple类型
l1 = [11,22,33,44,55]

em = enumerate(l1)

l2 = [i for i in em]
print(l2)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
[(0, 11), (1, 22), (2, 33), (3, 44), (4, 55)]

  
 
  • 1
em = enumerate(l1, start=100)

l2 = [i for i in em]
print(l2)

  
 
  • 1
  • 2
  • 3
  • 4
[(100, 11), (101, 22), (102, 33), (103, 44), (104, 55)]

  
 
  • 1

collections模块

  • namedtuple
  • deque

namedtuple

  • tuple类型
  • 是一个可命名的tuple
import collections
Point = collections.namedtuple("Point", ['x', 'y'])
p = Point(11, 22)
print(p.x)
print(p[0])

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
11
11

  
 
  • 1
  • 2
Circle = collections.namedtuple("Circle", ['x', 'y', 'r'])

c = Circle(100, 120, 20)
print(c)
print(type(c))

# 检测一下namedtuple到底属于谁的子类
isinstance(c, tuple)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
Circle(x=100, y=120, r=20)
<class '__main__.Circle'> True

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
import collections
help(collections.namedtuple)

  
 
  • 1
  • 2
Help on function namedtuple in module collections:

namedtuple(typename, field_names, *, rename=False, defaults=None, module=None) Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', ['x', 'y']) >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords >>> p[0] + p[1] # indexable like a plain tuple 33 >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) >>> p.x + p.y # fields also accessible by name 33 >>> d = p._asdict() # convert to a dictionary >>> d['x'] 11 >>> Point(**d) # convert from a dictionary Point(x=11, y=22) >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields Point(x=100, y=22)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

deque

  • 比较方便的解决了频繁删除插入带来的效率问题
from collections import deque

q = deque(['a', 'b', 'c'])
print(q)

q.append('d')
print(q)

q.appendleft('x')
print(q)


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
deque(['a', 'b', 'c'])
deque(['a', 'b', 'c', 'd'])
deque(['x', 'a', 'b', 'c', 'd'])

  
 
  • 1
  • 2
  • 3

defaultdict

  • 当直接读取dict不存在的属性时,直接返回默认值
d1 = {"one":1, "two":2, "three":3}
print(d1['one'])
print(d1['four'])

  
 
  • 1
  • 2
  • 3
1 ---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

<ipython-input-34-83fcfcbfcf65> in <module> 1 d1 = {"one":1, "two":2, "three":3} 2 print(d1['one'])
----> 3 print(d1['four'])


KeyError: 'four'

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
from collections import defaultdict
# lambda表达式,直接返回字符串
func = lambda: "ruochen"
d2 = defaultdict(func)

d2["one"] = 1
d2["two"] = 2

print(d2['one'])
print(d2['four'])

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
1
ruochen

  
 
  • 1
  • 2

Counter

  • 统计字符串个数
from collections import Counter

# 为什么下面结果不把abcd...作为键值,而是以其中每一个字母作为键值
# 需要括号里内容为可迭代
c = Counter("abcdeabcabc")
print(c)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
Counter({'a': 3, 'b': 3, 'c': 3, 'd': 1, 'e': 1})

  
 
  • 1
# help(Counter)

  
 
  • 1
s = {"I", "love", "you"}
c = Counter(s)

print(c)

  
 
  • 1
  • 2
  • 3
  • 4
Counter({'love': 1, 'you': 1, 'I': 1})

  
 
  • 1

文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。

原文链接:ruochen.blog.csdn.net/article/details/90736589

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。