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. 洛谷 P1280 尼克的任务题解

    题目链接:https://www.luogu.org/problem/P1280 题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每 ...

  2. mysql 5.6 rpm安装启动、配置参数、字符集修改等

    linux 7 安装mysql server 注意:此mysql版本是el6 MySQL-server-5.6.35-1.el6.x86_64 一.安装部署: 1.yum:首先要配置yum源,yum安 ...

  3. 51nod 2497 数三角形

    小b有一个仅包含非负整数的数组a,她想知道有多少个三元组(i,j,k),满足i<j<k且a[i],a[j],a[k]可能作为某个三角形的三条边的边长. 收起   输入 第一行输入一个正整数 ...

  4. 【转】Deep dive into pipe function in RxJS

    原文: https://codewithstyle.info/deep-dive-pipe-function-rxjs/ --------------------------------------- ...

  5. 在PHP中使用CURL实现GET和POST请求的方法

    1.CURL介绍 CURL是一个利用URL语法规定来传输文件和数据的工具.支持很多协议,如HTTP.FTP.TELNET等. 幸运的是PHP也支持CURL库.本文将介绍curl的一些高级特性,以及在P ...

  6. 学习Spring-Data-Jpa(四)---Naming命名策略,源码跟踪

    1.首先在Entity实体中,命名方式有两种: 一种是显示命名,即通过@Table的name属性指定对应的数据库表名称,@Column的name属性指定实体字段对应数据库字段的名称. 另一种是隐式命名 ...

  7. 011——MATLAB清除工作控件变量

    (一):参考文献:https://zhidao.baidu.com/question/234530287.html 清除当前工作空间全部变量:clear: 清除当前工作空间某些变量:clear 变量名 ...

  8. C# 异步的简单用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. C变量作用域的分类和优先级

    变量从高到低的优先级以下面展示: 1.文件作用域:变量在全局从文件开头到结尾一直有效即全局变量 2.函数作用域也称局部变量 3.代码块作用域:用{}花括号内的定义的变量:都是在代码块{}中有效 如:i ...

  10. 机器学习---用python实现最小二乘线性回归算法并用随机梯度下降法求解 (Machine Learning Least Squares Linear Regression Application SGD)

    在<机器学习---线性回归(Machine Learning Linear Regression)>一文中,我们主要介绍了最小二乘线性回归算法以及简单地介绍了梯度下降法.现在,让我们来实践 ...