文章目录
- 课程简介
- Quiz 9: Dictionaries
- 单选题(1-11)
- 编程题
- Exercise 9.4
课程简介
Python for Everybody 零基础程序设计(Python 入门)
- This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
- We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
- The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
- This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
- This course covers Python 3.
coursera
Python for Everybody 零基础程序设计(Python 入门)
Charles Russell Severance
Clinical Professor
个人主页
Twitter
University of Michigan
课程资源
coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站
PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网
Quiz 9: Dictionaries
The dictionary data structures allows us to store multiple values in an object and look up the values by their key.
单选题(1-11)
- How are Python dictionaries different from Python lists?
- Python dictionaries are a collection and lists are not a collection
- Python lists can store strings and dictionaries can only store words
- Python lists store multiple values and dictionaries store a single value
- Python lists maintain order and dictionaries do not maintain order
- How are Python dictionaries different from Python lists?
- Python lists are indexed using integers and dictionaries can use strings as indexes
- Python lists store multiple values and dictionaries store a single value
- Python dictionaries are a collection and lists are not a collection
- Python lists can store strings and dictionaries can only store words
- What is a term commonly used to describe the Python dictionary feature in other programming languages?
- Sequences
- Lambdas
- Closures
- Associative arrays
- What would the following Python code print out?
stuff = dict()
print(stuff['candy'])
- candy
- 0
- The program would fail with a traceback
- -1
- What would the following Python code print out?
stuff = dict()
print(stuff.get('candy',-1))
- The program would fail with a traceback
- ‘candy’
- -1
- 0
- (T/F) When you add items to a dictionary they remain in the order in which you added them.
- True
- False
- What is a common use of Python dictionaries in a program?
- Building a histogram counting the occurrences of various strings in a file
- Sorting a list of names into alphabetical order
- Splitting a line of input into words using a space as a delimiter
- Computing an average of a set of numbers
- Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
- counts[key] = (counts[key] * 1) + 1
- counts[key] = counts.get(key,0) + 1
- counts[key] = key + 1
- counts[key] = counts.get(key,-1) + 1
- counts[key] = (key in counts) + 1
- In the following Python, what does the for loop iterate through?
x = dict()
...
for y in x :
...
- It loops through the keys in the dictionary
- It loops through the values in the dictionary
- It loops through the integers in the range from zero through the length of the dictionary
- It loops through all of the dictionaries in the program
- Which method in a dictionary object gives you a list of the values in the dictionary?
- keys()
- items()
- each()
- values()
- all()
- What is the purpose of the second parameter of the get() method for Python dictionaries?
- The value to retrieve
- To provide a default value if the key is not found
- The key to retrieve
- An alternate key to use if the first key cannot be found
编程题
Exercise 9.4
题目:
Write a program to read through thembox-short.txt
and figure out who has sent the greatest number of mail messages. The program looks for 'From ’ lines and takes the second word of those lines as the person who sent the mail.
The program creates a Python dictionary that maps the sender’s mail address to a count of the number of times they appear in the file.
After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
[Desired Output]
cwen@iupui.edu 5
解题代码:
name = input("Enter file:") if len(name) < 1: name = "mbox-short.txt" handle = open(name) d = dict() for line in handle : ls = line.split() if line.startswith('From') and len(ls) == 7 : d[ls[1]] = d.get(ls[1], 0) + 1 bk = None bv = None for k,v in d.items() : if bv is None or v > bv : bv = v bk = k print(bk, bv)