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

歡迎訪問 生活随笔!

生活随笔

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

python

defaultdict python3,Python collections.defaultdict() 与 dict的使用和区别|python3教程|python入门|python教程...

發布時間:2025/3/12 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 defaultdict python3,Python collections.defaultdict() 与 dict的使用和区别|python3教程|python入门|python教程... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://www.xin3721.com/eschool/python.html

在Python里面有一個模塊collections,解釋是數據類型容器模塊。這里面有一個collections.defaultdict()經常被用到。主要說說這個東西。

綜述:

這里的defaultdict(function_factory)構建的是一個類似dictionary的對象,其中keys的值,自行確定賦值,但是values的類型,是function_factory的類實例,而且具有默認值。比如default(int)則創建一個類似dictionary對象,里面任何的values都是int的實例,而且就算是一個不存在的key, d[key] 也有一個默認值,這個默認值是int()的默認值0.

defaultdict

dict subclass that calls a factory function to supply missing values。

這是一個簡短的解釋

defaultdict屬于內建函數dict的一個子類,調用工廠函數提供缺失的值。

比較暈,什么是工廠函數:

來自python 核心編程的解釋

Python 2.2 統一了類型和類, 所有的內建類型現在也都是類, 在這基礎之上, 原來的

所謂內建轉換函數象int(), type(), list() 等等, 現在都成了工廠函數。 也就是說雖然他

們看上去有點象函數, 實質上他們是類。當你調用它們時, 實際上是生成了該類型的一個實

例, 就象工廠生產貨物一樣。

下面這些大家熟悉的工廠函數在老的Python 版里被稱為內建函數:

int(), long(), float(), complex()

str(), unicode(), basestring()

list(), tuple()

type()

以前沒有工廠函數的其他類型,現在也都有了工廠函數。除此之外,那些支持新風格的類

的全新的數據類型,也添加了相應的工廠函數。下面列出了這些工廠函數:

dict()

bool()

set(), frozenset()

object()

classmethod()

staticmethod()

super()

property()

file()

再看看它的使用:

這里就開始有點明白了,原來defaultdict可以接受一個內建函數list作為參數。其實呢,list()本身是內建函數,但是再經過更新后,python里面所有東西都是對象,所以list改編成了類,引入list的時候產生一個類的實例。

還是不太明白,再看defaultdict的help解釋

class collections.defaultdict([default_factory[, ...]])

Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.

首先說了,collections.defaultdict會返回一個類似dictionary的對象,注意是類似的對象,不是完全一樣的對象。這個defaultdict和dict類,幾乎是一樣的,除了它重載了一個方法和增加了一個可寫的實例變量。(可寫的實例變量,我還是沒明白)

The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

defaultdict objects support the following method in addition to the standard dict operations:

__missing__(key)

If the default_factory attribute is None, this raises a KeyError exception with the key as argument.

If default_factory is not None, it is called without arguments to provide a default value for the given key, this value is inserted in the dictionary for the key, and returned.

主要關注這個話,如果default_factory不是None, 這個default_factory將以一個無參數的形式被調用,提供一個默認值給___missing__方法的key。 這個默認值將作為key插入到數據字典里,然后返回。

十分暈。有扯出了個__missing__方法,這個__missing__方法是collections.defaultdict()的內建方法。

If calling default_factory raises an exception this exception is propagated unchanged.

This method is called by the __getitem__() method of the dict class when the requested key is not found; whatever it returns or raises is then returned or raised by __getitem__().

Note that __missing__() is not called for any operations besides __getitem__(). This means that get() will, like normal dictionaries, return None as a default rather than using default_factory.

defaultdict objects support the following instance variable:

default_factory

This attribute is used by the __missing__() method; it is initialized from the first argument to the constructor, if present, or to None, if absent.

看樣子這個文檔是難以看懂了。直接看示例:

總結

以上是生活随笔為你收集整理的defaultdict python3,Python collections.defaultdict() 与 dict的使用和区别|python3教程|python入门|python教程...的全部內容,希望文章能夠幫你解決所遇到的問題。

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