Dictionary as a set of counters

Suppose you are given a string and you want to count how many times each letters appears. There are several ways you do it:

  1. You could create 26 variables, one for each letter of the alphabet. Then you could traverse the string and, for each character, increment the corresponding counter, probably using a chained conditional.
  2. You could create a list with 26 elements. Then you could convert each character to a number (using the built-in function ord), use the number as an index into the list, and increment the appropriate counter.
  3. You could create a dictionary with characters as keys and counters as the corresponding values. The first time you see a character, you would add an item to the dictionary. After that you would increment the value of an existing item.

Each of these options performs the same computation, but each of them implements that computation in a different way. An implementation is a way of performing a computation; some implementations are better than others. For example, an advantage of the dictionary implementation is that we don’t have to know ahead of time which letters appear in the string and we only have to make room for the letters that do appear. Here we can use the word list in word study.

Dictionaries have a method called get that takes a key and a default value. If the key appears in the dictionary, get returns the corresponding value; otherwise it returns the default value.

from Thinking in Python

Dictionary as a set of counters

时间: 2024-11-10 07:44:19

Dictionary as a set of counters的相关文章

我要翻译《Think Python》-002 贡献列表 & 目录部分

PDF源文件地址 :  http://www.greenteapress.com/thinkpython/thinkpython.pdf 贡献列表 自从本书诞生之后,有超过上百个目光敏锐且有想法的读者给我发来了许多建议并指出了一些需要修正的地方.他们的热情和无私的奉献给了我巨大的帮助.如果你有任何建议或者发现需要修正的地方,请发邮件至:[email protected].如果您的建议被采纳,您的大名将会出现在我们的贡献人员列表(除非你本人过于低调拒绝承认).如果您发现文中的错误内容,敬请提供一下

C#中Dictionary的介绍

关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionary.html 说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键必须是唯一的,而值不需要唯一的     键和值都可以是任何类型(比如:string, int, 自定义类型,等等

c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展

Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多时候,我们是不敢轻易添加的,因为 Dictionary<TKey, TValue>不允许重复,尝试添加重复的键时 Add 方法引发 ArgumentException. 大多时候,我们都会写成以下的样子: var dict = new Dictionary<int, string>()

Linq在Array,List,Dictionary中的应用

Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Text.RegularExpressions; 6 7 namespace Test 8 { 9 cl

.Net——缓存机制(一):利用Dictionary模拟缓存

在计算机的硬件设计中,有一个被反复使用的思想--缓存.同样,在软件设计中,这个思想也可以用来解决数据读取非常耗时带来的性能问题(当然,在时间和空间上,我们要寻找一个平衡点). 首先来看理想的缓存应该是怎么描述的: static Func<T, R> Cache<T, R>(Func<T, R> func) { var mem = new Dictionary<T, R>(); return x => { if (!mem.ContainsKey(x))

JavaScript如何创建dictionary对象

对于JavaScript来说,其自身的Array对象仅仅是个数组,无法提供通过关键字来获取保存的数据,jQuery源码中提供了一种非常好的方式来解决这个问题,先看一下源码: function createCache() { var keys = []; function cache(key, value) { // Use (key + " ") to avoid collision with native prototype // properties (see Issue #157

Dictionary Learning(字典学习、稀疏表示以及其他)

第一部分 字典学习以及稀疏表示的概要 字典学习(Dictionary Learning)和稀疏表示(Sparse Representation)在学术界的正式称谓应该是稀疏字典学习(Sparse Dictionary Learning).该算法理论包含两个阶段:字典构建阶段(Dictionary Generate)和利用字典(稀疏的)表示样本阶段(Sparse coding with a precomputed dictionary).这两个阶段(如下图)的每个阶段都有许多不同算法可供选择,每种

Learn Python 009: Dictionary

# create a dictionary students = {"Alice": 24, "Bob": 26, "Clark": 23, "Dan": 28, "Emma": 31} # add entry to a dictionary students['Fred'] = 27 # alter an entry students['Alice'] = 25 # delete entry del st

基础才是重中之重~Dictionary&lt;K,V&gt;里V的设计决定的性能

回到目录 字典对象Dictionary<K,V>我们经常会用到,而在大数据环境下,字典使用不当可能引起性能问题,严重的可能引起内在的溢出! 字典的值建议为简单类型,反正使用Tuple<T> 字典的键在查找时,时间复杂度为O(1),性能不会有任何问题,所以不要愿望它 下面代码是对500万的字典进行测试,首先赋值,然后取出一个随机机,性能在毫秒级 static void Draw() { int count = 5000000; Console.WriteLine("test