Quiz 10: Tuples | Python for Everybody 配套练习_解题记录


课程简介

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 10: Tuples

The tuple is a Python data structure that is like a simple and efficient list.


单选题(1-11)

  1. What is the difference between a Python tuple and Python list?
  • Lists are indexed by integers and tuples are indexed by strings
  • Tuples can be expanded after they are created and lists cannot
  • Lists are mutable and tuples are not mutable
  • Lists maintain the order of the items and tuples do not maintain order
  1. Which of the following methods are available for both Python lists and Python tuples?
  • sort()
  • append()
  • index()
  • reverse()
  • pop()
  1. What will end up in the variable y after this code is executed?
x , y = 3, 4
  • 3
  • A two item list
  • A dictionary with the key 3 mapped to the value 4
  • A two item tuple
  • 4
  1. In the following Python code, what will end up in the variable y?
x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
y = x.items()
  • A tuple with three integers
  • A list of integers
  • A list of tuples
  • A list of strings
  1. Which of the following tuples is greater than x in the following Python sequence?
x = (5, 1, 3)
if ??? > x :
   ...
  • (6, 0, 0)
  • (0, 1000, 2000)
  • (4, 100, 200)
  • (5, 0, 300)
  1. What does the following Python code accomplish, assuming the c is a non-empty dictionary?
tmp = list()
for k, v in c.items() :
    tmp.append( (v, k) )
  • It computes the average of all of the values in the dictionary
  • It computes the largest of all of the values in the dictionary
  • It sorts the dictionary based on its key values
  • It creates a list of tuples where each tuple is a value, key pair
  1. If the variable data is a Python list, how do we sort it in reverse order?
  • data = sortrev(data)
  • data.sort(reverse=True)
  • data.sort.reverse()
  • data = data.sort(-1)
  1. Using the following tuple, how would you print ‘Wed’?
days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
  • print(days{2})
  • print[days(2)]
  • print(days[1])
  • print(days[2])
  • print(days(2))
  • print(days.get(1,-1))
  1. In the following Python loop, why are there two iteration variables (k and v)?
c = {'a':10, 'b':1, 'c':22}
for k, v in c.items() :
    ...
  • Because the keys for the dictionary are strings
  • Because the items() method in dictionaries returns a list of tuples
  • Because for each item we want the previous and current key
  • Because there are two items in the dictionary
  1. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?
  • For a temporary variable that you will use and discard without modifying
  • For a list of items that want to use strings as key values instead of integers
  • For a list of items you intend to sort in place
  • For a list of items that will be extended as new items are found

编程题

Exercise 10.2

题目:
Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages.
You can pull the hour out from the 'From ’ line by finding the time and then splitting the string a second time using a colon.

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

[Desired Output]

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

解题代码:

>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 :
       lh = ls[5].split(':')
       d[lh[0]] = d.get(lh[0], 0) + 1

lkv = sorted([(k, v) for k, v in d.items()], reverse=False)

for k, v in lkv:
   print(k, v)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰.封万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值