Coursera课程《Python Data Structures》 密歇根大学 Charles Severance
Week5 Dictionary
9.1 Dictionaries
字典就像是一个包,而这个包里的每样东西都整整齐齐地贴好了标签,于是我们可以通过标签来找到我们想要的东西。而且注意,字典这个包是无序的,所以它不能根据顺序索引,只能根据标签。
>>> purse = dict()
>>> purse['money'] = 12
>>> purse['candy'] = 3
>>> purse['tissues'] = 75
>>> print(purse)
{'money':12, 'tissues':75, 'candy':3}
>>> print(purse['candy'])
3
>>> purse['candy'] = purse['candy'] + 2
>>> print(purse)
{'money':12, 'tissues':75, 'candy': 5}
9.2 Counting with Dictionaries
我们可以使用"in"来查看,是否某个key在这个字典里。
>>> ccc = dict()
>>> print(ccc['csev'])
Traceback (most recent call last):
File "
KeyError: 'csev'
>>> 'csev' in ccc
False
字典的get()方法是查看是否一个key已经在字典里,如果没有在的话,那就新建一个key,把它附上默认值。
比如下面这两段代码,其实功能是一样的。
if name in counts:
x = counts[name]
else:
x = 0
x = counts.get(name, 0)
所以使用get()来计数的话会更加方便。
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names:
counts[name] = counts.get(name, 0) + 1
print(counts)
输出结果就是
{'csev':2,'zqian':1,'cwen':2}
9.3 Dictionaries and Files
计数的一个基本模式是这样的。
counts = dict()
print('Enter a line of text:')
line = input('')
words = line.split()
print('Words:', words)
print('Counting...')
for word in words:
counts[word] = counts.get(word, 0) + 1
print('Counts', counts)
我们可以使用keys(), values(), items()这些方法来分别获得字典的key,值或者全部。
>>> jjj = {'chuck':1, 'fred':42, 'jan':100}
>>> print(list(jjj))
['jan', 'chuck', 'fred']
>>> print(jjj.keys())
['jan', 'chuck', 'fred']
>>> print(jjj.values())
[100, 1, 42]
>>> print(jjj.items())
[('jan', 100), ('chuck', 1), ('fred', 42)]
我们还可以使用两个循环变量同时对key和value进行循环。
jjj = {'chuck':1, 'fred':42, 'jan': 100}
for aaa,bbb in jjj.item():
print(aaa,bbb)
那么接下来我们就可以读取文件,来对里面的文本进行计数了。
name = input('Enter file:')
handle = open(name)
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word, 0) + 1
bigcount = None
bigword = None
for word, count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)
9.4 Assignment
作业的代码如下
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
words = line.split()
if len(words) < 1 or words[0] != 'From':
continue
counts[words[1]] = counts.get(words[1], 0) + 1
bigcount = None
bigword = None
for word, count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)