Python 101: defaultdict

defaultdict is a subclass of Python’s built-in dict class. It is part of the collections module and provides all the functionalities of a regular dictionary with an additional feature: if you try to access or modify a key that does not exist in the dictionary, defaultdict automatically creates the key with a default value of your choosing.

defaultdict 是 Python 内置 dict 类的子类。它是 collections 模块的一部分,提供了常规字典的所有功能,并具有一个附加功能:如果尝试访问或修改字典中不存在的键,defaultdict 会自动创建该键并赋予一个您选择的默认值。

语法

Syntax

from collections import defaultdict

# Syntax to create a defaultdict
d = defaultdict(default_factory)
  • default_factory: A function that provides the default value for the dictionary. This could be int, list, str, set, or any other callable that returns a default value.

  • default_factory: 一个提供字典默认值的函数。这可以是 intliststrset 或任何其他返回默认值的可调用对象。

示例

Example

from collections import defaultdict

# Example 1: defaultdict with int
int_dict = defaultdict(int)
int_dict['a'] += 1
print(int_dict)  # Output: defaultdict(<class 'int'>, {'a': 1})

# Example 2: defaultdict with list
list_dict = defaultdict(list)
list_dict['a'].append(1)
list_dict['a'].append(2)
print(list_dict)  # Output: defaultdict(<class 'list'>, {'a': [1, 2]})

# Example 3: defaultdict with str
str_dict = defaultdict(str)
str_dict['a'] += 'hello'
print(str_dict)  # Output: defaultdict(<class 'str'>, {'a': 'hello'})

解释

  • Example 1: The defaultdict is initialized with int as the default_factory. If you access a non-existent key, it automatically initializes the key with a default integer value 0. The line int_dict['a'] += 1 increments this default value, resulting in {'a': 1}.
  • Example 2: Here, the defaultdict is initialized with list as the default_factory. If you access a non-existent key, it initializes the key with an empty list. Elements are then appended to the list associated with the key.
  • Example 3: The defaultdict is initialized with str as the default_factory. If you access a non-existent key, it initializes the key with an empty string.

Explanation:

  • 示例 1defaultdictint 作为 default_factory 初始化。如果您访问一个不存在的键,它会自动用默认整数值 0 初始化该键。int_dict['a'] += 1 这一行增加了此默认值,结果为 {'a': 1}
  • 示例 2:在这里,defaultdictlist 作为 default_factory 初始化。如果您访问一个不存在的键,它会用一个空列表初始化该键。然后将元素附加到与键关联的列表中。
  • 示例 3defaultdictstr 作为 default_factory 初始化。如果您访问一个不存在的键,它会用一个空字符串初始化该键。

defaultdict 的优势

Advantages of defaultdict

  1. 避免 KeyError:使用常规字典时,访问不存在的键会引发 KeyErrordefaultdict 通过自动创建键并赋予默认值来避免此错误。

  2. 简化代码defaultdict 可以简化代码,因为您无需在访问前检查键是否存在。

  3. 适合积累器defaultdict 非常适合用作计数器、列表、集合或其他需要初始化数据结构的积累器。

  4. Avoids KeyError: With a regular dictionary, accessing a non-existent key raises a KeyError. defaultdict avoids this error by automatically creating the key with a default value.

  5. Simplifies Code: defaultdict can simplify your code since you don’t need to check whether a key exists before accessing it.

  6. Great for Accumulators: defaultdict is perfect for use cases like counters, lists, sets, or other accumulators where you need to initialize data structures.

示例:使用 defaultdict 作为计数器

Example: Using defaultdict as a Counter

from collections import defaultdict

text = "hello world"
char_count = defaultdict(int)

for char in text:
    char_count[char] += 1

print(char_count)  # Output: defaultdict(<class 'int'>, {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

解释:在这个示例中,我们使用 defaultdict 来统计字符串中每个字符的出现次数。由于 defaultdict 自动为每个新字符初始化计数为 0,我们可以直接使用 += 1 来增加计数,而不需要检查键是否存在。

Explanation: In this example, we use defaultdict to count the occurrences of each character in a string. Since defaultdict automatically initializes the count for each new character to 0, we can directly use += 1 to increment the count without checking if the key exists.

结论

Conclusion

defaultdict is a powerful and flexible tool that extends the capabilities of Python’s standard dictionary. It simplifies coding patterns where dictionaries are used as accumulators, counters, or when working with complex data structures. By automatically handling missing keys, it prevents common errors and reduces the need for additional code to manage key initialization.

defaultdict 是一个功能强大且灵活的工具,扩展了 Python 标准字典的功能。它简化了字典用作累加器、计数器或处理复杂数据结构时的编码模式。通过自动处理缺失的键,它防止了常见错误,并减少了管理键初始化所需的额外代码。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *