Think Python: Chapter 11 Dictionaries

12 篇文章 0 订阅

目录

这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录

这是MIT官方编程教程中Python TutorialLoops and List Comprehensions的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》(Think Python, Chapter 11 Dictionaries

Python Tutorial :Loops and List Comprehensions

Chapter 11 Dictionaries

dictionary: A mapping from a set of keys to their corresponding values.
A dictionary is like a list, but more general. In a list, the indices(目录) have to be integers(整数); in a dictionary they can be (almost) any type.
(list里的目录必须都是整数,dictionary里的目录可以是任何类型)

You can think of a dictionary as a mapping(射影) between a set of indices (which are called keys) and a set of values. Each key maps to a value.(我们可以把dictionary看做是一系列目录(keys)和一系列value**s的对应关系) .The association of a key and a value is called a **key-value pair or sometimes an item.

**key-value pair: **The representation of the mapping from a key to a value.
item: Another name for a key-value pair.
key: An object that appears in a dictionary as the first part of a key-value pair.
value: An object that appears in a dictionary as the second part of a key-value pair. This is more specific than our previous use of the word “value.”

The function dict creates a new dictionary with no items. (用dict函数来创建一个dictionary)

>>> eng2sp=dict()
>>> print(eng2sp)
{}

The squiggly-brackets, {}, represent an empty dictionary.

To add items to the dictionary, you can use square brackets(将items加到dictionary中):

>>> eng2sp['one']='uno'
>>> print(eng2sp)
{'one': 'uno'}

如此表示在eng2sp这个dictionary中,one这个key对应的就是uno这个字符

The order of the key-value pairs is not the same. In fact, if you type the same example on your computer, you might get a different result. In general, the order of items in a dictionary is unpredictable.
(dictionary中key-value的排序与输入的顺序无关,里面的排序是随机的,不可预测的)

>>> eng2sp={'one':'uno','two':'dos','three':'tres'}
>>> print(eng2sp)
{'one': 'uno', 'three': 'tres', 'two': 'dos'}

其他的用法:

#输出
>>> print(eng2sp['one'])
uno

#长度
>>> len(eng2sp)
4

dictionary里的is的用法:

>>> 'one' in eng2sp
True

>>> 'uno' in eng2sp
False

dictionary里的is所判断的是keys是否存在,不是values

若要判断里面的values是否存在,可以先将values导出到数组(list),在用list的方式处理。例如:

>>> values=eng2sp.values()
>>> 'uno' in values
True
>>> print(values)
dict_values(['uno', 'dos', 'shishi', 'tres'])

11.1 Dictionary as a set of counters

Suppose you are given a string and you want to count how many times each letter appears. There are several ways you could do it**(统计一个string中各个letter出现的次数的三个方法)**:
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.(第一种是创建26个变量,分别统计各个字母出现的次数,使用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.(将不同字母代表成不同的数1-26,以数字作为list的index,再累增)
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.(用dictionary创建,以字母为keys,以计数值为values,当第一次遇到某个的字母的时候,在字典中添加一个空间,并累加,最后得到一个存储着的values):代码如下

#11.1 Dictionary as a set of counters
def histogram(s):
    d=dict()
    for c in s:
        if c not in d:
            d[c]=1
        else:
            d[c]+=1
    return d

#效果
histogram('dfajdfbajkdhfhkancahdljkfnadjkf')
{'d': 5, 'j': 4, 'n': 2, 'l': 1, 'c': 1, 'k': 4, 'f': 5, 'a': 5, 'b': 1, 'h': 3}

Each of these options performs the same computation, but each of them implements that computation in a different way.(这些方法的计算方式一样,只是累加的计算不一样而已)

implementation(实行,履行,安装): A way of performing a computation.
hashtable: The algorithm used to implement Python dictionaries.

11.2 Looping and dictionaries

#接上文案例
#11.2 Looping and dictionaries
def print_hist(s):
    for c in s:
        print(c,h[c])

#效果
>>> h=histogram('dfajdfbajkdhfhkancahdljkfnadjkf')
>>> print_hist(h)
c 1
k 4
a 5
h 3
j 4
l 1
d 5
b 1
n 2
f 5
Exercise 11.3. 使上题的输出有序
def print_hist(s):
    list1=[]#创建一个顺序的list
    for c in s:
        list1.append(c)#将dict中的keys加入到list1中
    list1.sort()#将list1排序

    for c in list1:
        print(c,s[c])

#输出效果
>>> print_hist(h)
a 5
b 1
c 1
d 5
f 5
h 3
j 4
k 4
l 1
n 2

11.3 Reverse lookup(根据value找keys)

lookup: A dictionary operation that takes a key and finds the corresponding value.(用一个key找到对应的value叫lookup)
**reverse lookup: **A dictionary operation that takes a value and finds one or more keys that map to it.(用value去找keys叫reverse lookup)

#11.3 Reverse lookup
def reserve_lookup(d,v):
    for k in d:
        if d[k]==v:
            return k
    raise ValueError

This function is yet another example of the search pattern, but it uses a feature we haven’t seen before, raise . The raise statement causes an exception; in this case it causes a ValueError, which generally indicates that there is something wrong with the value of a parameter.
raise语句表示的是产生排除,并对排除做出处理。在本例中,排除的结果是ValueError,表示参数的值有错误。

Exercise 11.4. 输出所有对应的keys
#Exercise 11.4. 输出所有对应的keys
def reserve_lookup(d,v):
    a=[]#用于储存查找到的keys
    for k in d:
        if d[k]==v:
            a.append(k)
    if a==[]:
        raise ValueError
    else:
        return a

#输出效果
>>> h=histogram('dfajdfbajkdhfhkancahdljkfnadjkf')
>>> print_hist(h)
d 5
n 2
l 1
j 4
k 4
h 3
c 1
a 5
f 5
b 1
>>> reserve_lookup(h,4)
['j', 'k']

11.4 Dictionaries and lists

Lists can appear as values in a dictionary. (list可以是dictionary的values,dictionary的values可以是任何类型,包括list)Lists can be values in a dictionary, as this example shows, but they cannot be keys. (但是list不能做dictionary的keys)

singleton: A list (or other sequence) with a single element.

(example:可以将list转换到dictionary中,那么可以根据list中的values查询相对应的keys):

#11.4 Dictionaries and lists
#将list转换为dictionary
def invert_dict(d):
    inverse=dict()
    for key in d:
        val=d[key]
        if val not in inverse:
            inverse[val]=[key]
            #注意这里将dictionary的value看作是一个list,所以需要用[]
        else:
            inverse[val].append(key)

#效果
>>> hist=histogram('parrot')
>>> print(hist)
{'a': 1, 'o': 1, 'r': 2, 'p': 1, 't': 1}
>>> inverse=invert_dict(hist)
>>> print(inverse)
{1: ['a', 'o', 'p', 't'], 2: ['r']}

I mentioned earlier that a dictionary is implemented(实施) using a hashtable and that means that the keys have to be hashable.(dictionary的keys必须是hashtable的)

hashable : A type that has a hash function. Immutable(不可变) types like integers, floats and strings are hashable; mutable types like lists and dictionaries are not.

hash function: A function used by a hashtable to compute the location for a key.(hash function 哈希函数指的是用于计算key位置的函数)

Exercise 11.5.(未完成)

Read the documentation of the dictionary method setdefault and use it to write a more concise version of invert_dict.

11.5 Memos(备忘录)

memo: A computed value stored to avoid unnecessary future computation.

call graph: A diagram that shows every frame created during the execution of a program, with an arrow from each caller to each callee.
histogram(统计): A set of counters.

If you played with the fibonacci function from Section 6.7, you might have noticed that the bigger the argument you provide, the longer the function takes to run. Furthermore, the run time increases very quickly.

One solution is to keep track of values that have already been computed by storing them in a dictionary. A previously computed value that is stored for later use is called a memo. Here is a “memoized” version of fibonacci :(将已经计算过的部分储存到dictionary,那么下次再计算到这几个部分的时候,就不用算下去了,可以直接从dict中调用出来,减少计算时间)

#这段代码需要进一步修改,只是示意一下
#先设定一个字典
known={0:0,1:1}
#设计fibonacci函数
def fibonacii(n):
    #如果以前这个n已经计算过,直接从dict中提取
    if n in known:
        return known[n]
    res=fibonacci(n-1)+fibonacci(n-2)
    #新计算的结果储存到dict中
    known[n]=res
    return res

11.6 Global variables

global variable(全局变量): A variable defined outside a function. Global variables can be accessed from any function.It is common to use global variables for flags ;

flag: A boolean variable used to indicate whether a condition is true.

declaration(声明): A statement like global that tells the interpreter something about a variable.

[请百度,注意全局变量和local variable之间的关系和差别,这里不做累述]
(在python中使用全局变量,调用全局变量时是需要声明使用的变量是global的,否则,系统会自动认为你设置的变量只是与全局变量同名的local变量.例:)

#11.6 Global variables
been_called=False

def example2():
    global been_called
    been_called=True

If the global value is mutable, you can modify it without declaring it.(但如果全局变量是可变的,如list和dictionary,那么不用申明也可使用,比如11.5中的known字典)

11.7 Long integers

python 3.0已取消,全部归到int中,不累述了.

11.8 Debugging(怎么debugging大型的数据库)

As you work with bigger datasets it can become unwieldy to debug by printing and checking data by hand. Here are some suggestions for debugging large datasets:
1. Scale down the input: If possible, reduce the size of the dataset(减少数据规模,首先慢慢减少数据规模来查找bug出现在哪里,再慢慢增大确定bug位置和具体问题). For example if the program reads a text file, start with just the first 10 lines, or with the smallest example you can find. You can either edit the files themselves, or (better) modify the program so it reads only the first n lines.If there is an error, you can reduce n to the smallest value that manifests the error, and then increase it gradually as you find and correct errors.
2. Check summaries and types:(检查统计类型 Instead of printing and checking the entire dataset, consider printing summaries of the data: for example, the number of items in a dictionary or the total of a list of numbers.A common cause of runtime errors is a value that is not the right type(有一个很常见的问题是由于某一个值的类型不正确). For debugging this kind of error, it is often enough to print the type of a value.
3. Write self-checks(自我检查函数): Sometimes you can write code to check for errors automatically. For example, if you are computing the average of a list of numbers, you could check that the result is not greater than the largest element in the list or less than the smallest. This is called a “sanity check” because it detects results that are “insane.” Another kind of check compares the results of two different computations to see if they are consistent. This is called a “consistency check.”
4. Pretty print the output(标准化输出): Formatting debugging output can make it easier to spot an error. We saw an example in Section 6.9. The pprint module provides a pprint function that displays built-in types in a more human-readable format.
5. Again, time you spend building scaffolding can reduce the time you spend debugging.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值