文档章节

默认字典defaultdict

o
 osc_1ee7cxmx
发布于 2018/08/06 21:27
字数 373
阅读 0
收藏 0
def

「深度学习福利」大神带你进阶工程师,立即查看>>>

默认字典defaultdict
defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。
class defaultdict(dict):
"""
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce
a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were
passed to the dict constructor, including keyword arguments.
"""
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D. """
pass
def __copy__(self, *args, **kwargs): # real signature unknown
""" D.copy() -> a shallow copy of D. """
pass
def __getattribute__(self, name): # real signature unknown; restored from __doc__
""" x.__getattribute__('name') <==> x.name """
pass
def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__
"""
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce
a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were
passed to the dict constructor, including keyword arguments.
# (copied from class doc)
"""
pass
def __missing__(self, key): # real signature unknown; restored from __doc__
"""
__missing__(key) # Called by __getitem__ for missing key; pseudo-code:
if self.default_factory is None: raise KeyError((key,))
self[key] = value = self.default_factory()
return value
"""
pass
def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass
def __repr__(self): # real signature unknown; restored from __doc__
""" x.__repr__() <==> repr(x) """
pass
default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""Factory for default value called by __missing__()."""
原生字典:
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = {}
for value in values:
if value>66:
if my_dict.has_key('k1'):
my_dict['k1'].append(value)
else:
my_dict['k1'] = [value]
else:
if my_dict.has_key('k2'):
my_dict['k2'].append(value)
else:
my_dict['k2'] = [value]
默认字典:
from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in values:
if value>66:
my_dict['k1'].append(value)
else:
my_dict['k2'].append(value)
defaultdict字典解决方法
下一篇: 浮点型float
o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

程序员职场:拥有一个学位将会在你的职业生涯中更加顺利!

1、作为程序员为什么要拥有学位? 很多情况下,作为程序员,学位是进入大公司的敲门砖。 现在很多大的科技公司,学位是硬性要求。 一般都是本科以上的学历,甚至有的必须是硕士以上学历。 如...

IT技术分享社区
03/03
12
0
varchar和nvarchar有什么区别? - What is the difference between varchar and nvarchar?

问题: Is it just that nvarchar supports multibyte characters? 只是nvarchar支持多字节字符吗? If that is the case, is there really any point, other than storage concerns, to us......

技术盛宴
39分钟前
5
0
用flutter给图片加个好看的遮罩层【flutter20个实例之六】

一、老套路,先看样式 左起图一是我业务中的样式,左起图二、三是下方源码展示样式(复制可直接运行,无额外组件引入) 二、讲解 1.结构拆分 我们先看下页面布局结构,首先肯定是有个GridVie...

一代码农码一代
40分钟前
17
0
世界上最美的瀑布在这里,太美了!

亲近大自然,高山流水遇知音,倾听心灵的声音。。。 声明:文章及图片、视频来自网络,如有版权方面的疑问请和我们联系,我们将在24小时内删除。 本文分享自微信公众号 - Python提升课堂(DJXY0...

花儿开放
2014/08/17
0
0
商城小程序制作流程

随着商城小程序的火爆,很多商家都迫不及待的想制作商城小程序,下面就和大家分享一下商城小程序制作流程? 第1步: 注册并认证小程序账号 注册并认证小程序账号,打开百度搜索,“微信公众平...

木鱼小铺小程序1
51分钟前
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部