美文网首页程序员Python
说说在 Python 字典中如何在读取不存在的键时得到一个默认值

说说在 Python 字典中如何在读取不存在的键时得到一个默认值

作者: deniro | 来源:发表于2020-11-21 17:11 被阅读0次

如果有方法能够在 Python 字典类型中,当读取不存在的键时能够得到一个默认值,那么代码就会变得更加直观。通过 defaultdict 类型可以实现这个目的1

我们来改写一个 “输出单词所在坐标” 的示例来说明使用 defaultdict 类型与使用 setDefault 方法之间的区别2

改写后的完整示例代码如下:

import collections
import logging

logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')

'''
defaultdict 示例
:author:Deniro Lee
'''

import sys
import re

WORD_RE = re.compile(r'\w+')

index = collections.defaultdict(list)
with open(sys.argv[1], encoding='utf-8') as fp:
    for line_no, line in enumerate(fp, 1):
        for match in WORD_RE.finditer(line):
            word = match.group()
            column_no = match.start() + 1
            location = (line_no, column_no)
            index[word].append(location)

for word in sorted(index, key=str.upper):
    logging.info('word -> %s;index[word] -> %s', word, index[word])

部分运行结果:

INFO - word -> a;index[word] -> [(1, 7), (1, 46), (1, 76), (1, 111), (3, 52), (3, 87), (19, 75), (19, 162)]
INFO - word -> about;index[word] -> [(9, 83)]
INFO - word -> acknowledged;index[word] -> [(1, 27)]

我们把这两种方法在示例中实际区别列举出来,这样看的更清楚。

使用 setDefault 方法:

index = {}
...
index.setdefault(word, []).append(location)
...

使用 defaultdict 类型:

index = collections.defaultdict(list)
index[word].append(location)

相对来说,defaultdict 类型的写法比使用 setDefault 方法的写法更加直观。

在 index[word] 表达式中,如果 word 在 index 中不存在,即字典的某个键不存在,那么会新建一个 list 作为 word 的值,并以 word 为键一并放入 index 字典中,相当于初始化操作。所以示例中可以直接把 location 对象append 到 index[word] 表达式中。


参考资料:
【1】Luciano Ramalho (作者),安道,吴珂 (译者).流畅的Python.人民邮电出版社,2017:143-145.
【2】说说在 Python 中如何使用 setDefault 方法提高效率.

相关文章

网友评论

    本文标题:说说在 Python 字典中如何在读取不存在的键时得到一个默认值

    本文链接:https://www.haomeiwen.com/subject/ldreiktx.html