《Python Data Structures》Week5 Dictionary 课堂笔记
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 "<stdin", line 1, in <module>
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)
《Python Data Structures》Week5 Dictionary 课堂笔记的更多相关文章
- 【Python学习笔记】Coursera课程《Python Data Structures》 密歇根大学 Charles Severance——Week6 Tuple课堂笔记
Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week6 Tuple 10 Tuples 10.1 Tuples A ...
- 《Python Data Structures》 Week4 List 课堂笔记
Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week4 List 8.2 Manipulating Lists 8 ...
- 潭州课堂25班:Ph201805201 爬虫基础 第七课 Python与常见加密方式 (课堂笔记)
打开图形界面 18版 Python与常见加密方式 前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时 ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Python Tutorial 学习(五)--Data Structures
5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...
- 《Data Structures and Algorithm Analysis in C》学习与刷题笔记
<Data Structures and Algorithm Analysis in C>学习与刷题笔记 为什么要学习DSAAC? 某个月黑风高的夜晚,下班的我走在黯淡无光.冷清无人的冲之 ...
- ocp11g培训内部教材_052课堂笔记(042)_体系架构
OCP 052 课堂笔记 目录 第一部分: Oracle体系架构... 4 第一章:实例与数据库... 4 1.Oracle 网络架构及应用环境... 4 2.Oracle 体系结构... 4 3. ...
随机推荐
- “百度杯”CTF比赛 十一月场--CrackMe01
测试文件:https://static2.ichunqiu.com/icq/resources/fileupload/CTF/BSRC/BSRC-11-3/CrackMe01_97D2BF0DBD2E ...
- ECMAScript 6 学习笔记(二)
ECMAScript 6 let和const命令 let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a ...
- 计算机编号、硬盘序列号和Mac地址查询方法
(1)计算机编号: SN也就是Serial Number的缩写,中文也就是产品序列号,而电脑的后面一般也有一个这样的SN序列号,那么怎么查看电脑的S/N序列号呢? 方法一: 将笔记本电脑翻过来,然后在 ...
- Linux下用命令查看CPU ID以及厂家等信息
Linux下用命令查看CPU ID // 获得CPU IDdmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}' // 获得磁盘IDfdi ...
- ASP.NET Core 基础知识(五) 依赖关系注入
参考https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html 和 https://www.cnblogs.com/artech/p/net-c ...
- mysql——批量插入数据
要测试一下新功能,需要测试环境下的数据库有大量的数据,一个个插入显然不现实,需要了解一下存储过程 https://www.cnblogs.com/endtel/p/5407455.html Navic ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- react native之封装离线缓存框架
请求数据=>本地有无缓存+缓存数据是否过期 =>可用 =>不可用 将代码封装成一个DataStore.js文件, 这里面主要提供:从本地获取数据,从网络获取数据,创建本地时间戳,请求 ...
- SQL server 字段合并CAST(org_no AS VARCHAR(20))+CAST(page_no AS VARCHAR(20))+CAST(djlb_no AS VARCHAR(20)))
sql server 字段合并(CAST) ---------------------- select (CAST(org_no AS VARCHAR(20))+CAST(page_no AS VAR ...
- M(model)V(view)C(controller,serlvet),(分) 静态工厂模式,单例模式