日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 高级函数补充

發布時間:2023/12/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 高级函数补充 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

補充幾個高級函數

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 高级函数补充的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。