user_dict = {}
users = ["baoshan1", "baoshan2", "baoshan3","baoshan1", "baoshan2", "baoshan2"]
for user in users:
if user not in user_dict:
user_dict[user] = 1
else:
user_dict[user] += 1
print(user_dict) user_dict = {}
users = ["baoshan1", "baoshan2", "baoshan3","baoshan1", "baoshan2", "baoshan2"]
for user in users:
user_dict.setdefault(user, 0)
user_dict[user] += 1
print(user_dict) # 不需要做if else的判断
# 效率高,少一次user_dict的查询操作 from collections import defaultdict
user_dict = defaultdict(int)
users = ["baoshan1", "baoshan2", "baoshan3","baoshan1", "baoshan2", "baoshan2"]
for user in users:
user_dict[user] += 1
print(user_dict)
# defaultdict的好处,传递可调用的对象例如int、list、函数等

Python的collections之defaultdict的使用及其优势的更多相关文章

  1. Python的collections之namedtuple的使用及其优势

    类实现: class User: def __init__(self, name, age, height): self.name = name self.age = age self.height ...

  2. python之collections模块(OrderDict,defaultdict)

    前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...

  3. Python中collections.defaultdict()使用

    一个小示例 from collections import defaultdict import json def tree(): return defaultdict(tree) users = t ...

  4. 再谈collections模块defaultdict()和namedtuple()

    defaultdict()和namedtuple()是collections模块里面2个很实用的扩展类型.一个继承自dict系统内置类型,一个继承自tuple系统内置类型.在扩展的同时都添加了额外的很 ...

  5. python初探-collections容器数据类型

    collections容器数据类型是对基本数据类型的补充,简单介绍下计数器.有序字典.默认字典.可命名元祖.队列. 计数器(Counter) Counter是对字典类型的补充,用于追踪值得出现次数 c ...

  6. python的Collections 模块

    Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...

  7. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  8. Python 模块collections

    1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...

  9. 不可不知的Python模块: collections

    原文:http://www.zlovezl.cn/articles/collections-in-python/ Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想 ...

随机推荐

  1. 局部敏感哈希算法(Locality Sensitive Hashing)

    from:https://www.cnblogs.com/maybe2030/p/4953039.html 阅读目录 1. 基本思想 2. 局部敏感哈希LSH 3. 文档相似度计算 局部敏感哈希(Lo ...

  2. CSS 相邻元素选择器

    相邻兄弟选择器(Adjacent sibling selector)可选择紧接在另一元素后的元素,且二者有相同父元素.选择相邻兄弟 如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使 ...

  3. Linux PAM 之cracklib模块

       如何在Linux系统中限制密码长度的同时对密码的复杂程度也进行管理,最近发现有人的密码符合长度规则,但是却很简单很容易被猜出来,查了相关资料后发现了PAM中的pam_cracklib模块就是用来 ...

  4. python内建函数和工厂函数的整理

    内建函数参阅: https://www.cnblogs.com/pyyu/p/6702896.html 工厂函数: 本篇博文比较粗糙,后续会深入整理

  5. virtualbox下装ubuntu全屏问题

    有两种解决方法: 1.安装增强功能. 2.使用命令: sudo apt-get install virtualbox-guest-dkms 之后需要重启虚拟机,修改分辨率.

  6. PHP流程控制之分支结构switch语句的使用

    分支结构switch语句的使用 还记得我们最开始讲了这么一个故事: 王同学家里头特别有钱,所以他的行程方式和正常人的又有些不一样. 他的出行方式呢有6种,如下: 1,司机开车2,民航3,自己家的专机4 ...

  7. 流媒体知识 wiki

    媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等.最近因为项目的关系,需要理清媒 ...

  8. 目录——创建、切换、pwd、删除、复制、剪切

    1.创建目录: (1)在已经存在的目录下新建一个目录: 可以看出在创建1997目录后,在tmp中能够顺利找到. (2)在一个不存在的目录下新建一个目录: 直接在tmp目录下新建一个a目录,再在a目录下 ...

  9. Centos 7 安装 dotnet 环境

    Centos 7 安装  dotnet 环境 下载官方 rpm yum 源 直接 yum install 安装rpm -Uvh https://packages.microsoft.com/confi ...

  10. luogu 3246 莫队+RMQ+单调栈

    hnoi 2016 标签:题解 莫队 考虑左端点左移以及右端点右移产生的贡献 这样就可以由 \([l, r]\) 转移到另外的 \(4\) 个区间 \(f_{l, r}\) 表示右端点在 \(r\), ...