赞
踩
- # -*- coding: utf-8 -*-
- """
- Created on Tue May 17 15:19:30 2022
- @author: 12506
- """
-
- #还有dic.get()方法,看参考
-
-
- # =============================================================================
- # # 这种是会报错的,因为字典中没有key的记录
- # strings = ('puppy', 'kitten', 'puppy', 'puppy',
- # 'weasel', 'puppy', 'kitten', 'puppy')
- # counts = {}
- #
- # for kw in strings:
- # counts[kw] += 1
- # =============================================================================
-
- # 改进1:思想:第一次统计时候设置默认值1
- # =============================================================================
- # strings = ('puppy', 'kitten', 'puppy', 'puppy',
- # 'weasel', 'puppy', 'kitten', 'puppy')
- # counts = {}
- #
- # for kw in strings:
- # if kw not in counts:
- # counts[kw] = 1
- # else:
- # counts[kw] += 1
- #
- # # counts:
- # # {'puppy': 5, 'weasel': 1, 'kitten': 2}
- #
- # =============================================================================
-
- # 改进2:使用setdefault()函数:第一个参数是key的名称,第二个参数是默认值
- # =============================================================================
- # strings = ('puppy', 'kitten', 'puppy', 'puppy',
- # 'weasel', 'puppy', 'kitten', 'puppy')
- # counts = {}
- #
- # for kw in strings:
- # counts[kw] = counts.setdefault(kw, 0) + 1
- # =============================================================================
-
-
- # 以上方法已可以解决 字典默认值问题
- # 下面给出一种字典本身提供的默认值功能:collections.defaultdict(factory_function)
- # factory_function可以是list、set、str、int:当key不存在时候,返回默认值
-
- # =============================================================================
- # from collections import defaultdict
- #
- # dict1 = defaultdict(int)
- # dict2 = defaultdict(set)
- # dict3 = defaultdict(str)
- # dict4 = defaultdict(list)
- # dict1[2] = 'two'
- #
- # print(dict1[1])
- # print(dict2[1])
- # print(dict3[1])
- # print(dict4[1])
- #
- # # output:
- # # 0
- # # set()
- # #
- # # []
- # # dict1: defaultdict(int, {2: 'two', 1: 0})
- # =============================================================================
- # =============================================================================
- # from collections import defaultdict
- # # collections 类似一个类,用来初始化一个指定类型的字典
- # dic = defaultdict(list)
- # # dic: defaultdict(list, {})
- # dic['a']
- # # dic: defaultdict(list, {'a': []})
- # dic['b'].append('qq')
- # # dic: defaultdict(list, {'a': [], 'b': ['qq']})
- # dic.get('ss') # dict.get返回指定键的值,如果值不在字典中返回default值
- # dic['ss']
- # # dic: defaultdict(list, {'a': [], 'b': ['qq'], 'ss': []})
- # =============================================================================
-
- # =============================================================================
- # # 上面的单词统计
- # from collections import defaultdict
- #
- # strings = ('puppy', 'kitten', 'puppy', 'puppy',
- # 'weasel', 'puppy', 'kitten', 'puppy')
- # counts = defaultdict(lambda: 0) # 使用lambda来定义简单的函数
- #
- # for s in strings:
- # counts[s] += 1
- # # counts: defaultdict(<function __main__.<lambda>()>, {'puppy': 5, 'kitten': 2, 'weasel': 1})
- # =============================================================================
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。