最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python3中setdefault的用法介绍(代码)

来源:动视网 责编:小采 时间:2020-11-27 14:21:08
文档

python3中setdefault的用法介绍(代码)

python3中setdefault的用法介绍(代码):本篇文章给大家带来的内容是关于python3中setdefault的用法介绍(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。当字典 d[k]找不到正确的键时,Python会抛出异常,有没有一种优雅的方法来避免这种情况呢答案是肯定的.index
推荐度:
导读python3中setdefault的用法介绍(代码):本篇文章给大家带来的内容是关于python3中setdefault的用法介绍(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。当字典 d[k]找不到正确的键时,Python会抛出异常,有没有一种优雅的方法来避免这种情况呢答案是肯定的.index


本篇文章给大家带来的内容是关于python3中setdefault的用法介绍(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

当字典 d[k]找不到正确的键时,Python会抛出异常,有没有一种优雅的方法来避免这种情况呢?答案是肯定的.

index0.py 从索引中获取单词出现的频率信息,并写入列表 --没有使用dict.setdefault

#!/usr/bin/env python
# coding=utf-8
import sys, re

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

index = {}
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)
 occurrences = index.get(word, [])
 occurrences.append(location)
 index[word] = occurrences

for word in sorted(index, key=str.upper):
 print(word, index[word])

zen.txt

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

执行 python3 index0.py zen.txt

a [(19, 48), (20, 53)]
Although [(11, 1), (16, 1), (18, 1)]
ambiguity [(14, 16)]
and [(15, 23)]
are [(21, 12)]
aren [(10, 15)]
at [(16, 38)]
bad [(19, 50)]
be [(15, 14), (16, 27), (20, 50)]
beats [(11, 23)]
Beautiful [(3, 1)]
better [(3, 14), (4, 13), (5, 11), (6, 12), (7, 9), (8, 11), (17, 8), (18, 25)]
break [(10, 40)]
by [(1, 20)]
cases [(10, 9)]
...

index.py 使用了dict.setdefault 只用了一行就解决了获取和更新单词的出现情况列表

#!/usr/bin/env python
# coding=utf-8
import sys, re

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

index = {}
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.setdefault(word, []).append(location)

for word in sorted(index, key=str.upper):
 print(word, index[word])

也就是说:

my_dict.setdefault(key, []).append(new_value)

等价于

if key not in my_dict:
 my_dict[key] = []
my_dict[key].append(new_value)

二者效果相同,只是setdefault只需一次就完成整个操作,而后者需要进行两次查询

相关推荐:

在Python中操作字典之setdefault()方法的使用

Python3里的super()和__class__使用介绍

文档

python3中setdefault的用法介绍(代码)

python3中setdefault的用法介绍(代码):本篇文章给大家带来的内容是关于python3中setdefault的用法介绍(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。当字典 d[k]找不到正确的键时,Python会抛出异常,有没有一种优雅的方法来避免这种情况呢答案是肯定的.index
推荐度:
标签: 使用 用法 介绍
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top