Quiz 9: Dictionaries | Python for Everybody 配套练习_解题记录

news2025/1/9 16:31:12

文章目录

  • 课程简介
      • 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)

  1. 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
  1. 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
  1. What is a term commonly used to describe the Python dictionary feature in other programming languages?
  • Sequences
  • Lambdas
  • Closures
  • Associative arrays
  1. What would the following Python code print out?
stuff = dict()
print(stuff['candy'])
  • candy
  • 0
  • The program would fail with a traceback
  • -1
  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
  1. (T/F) When you add items to a dictionary they remain in the order in which you added them.
  • True
  • False
  1. 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
  1. 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
  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
  1. Which method in a dictionary object gives you a list of the values in the dictionary?
  • keys()
  • items()
  • each()
  • values()
  • all()
  1. 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 the mbox-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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/701682.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

conda的多线程下载工具mamba(解决Anaconda3 solving environment 巨慢的方法)

solving environment为什么会越来越慢&#xff1f; 根据原博的解释以及我查阅的相关资料&#xff0c;这是由于conda在新安装一个包或者更新包时需要搜索当前环境中所有的包的依赖空间&#xff0c;以找到满足所有依赖项的版本&#xff0c;随着用户安装的包越来越多&#xff0c;…

C#核心知识回顾——1.结构体、构造函数、GC、成员属性、索引器

1.结构体&#xff1a; 在 C# 中&#xff0c;结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。例如我定义了一个结构体&#xff0c;它有两个变量&#xff0c;创建一个这个类型的结构体&#xff0c;通过一个变量名调用多个变量&#xff0c;这些变量可…

Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】

文章目录 1. 界面实现2. JS具体实现2.1 第一种实现2.2 第二种实现 1. 界面实现 <input id"Date_select" type"text" class"form-control" placeholder"请选择时间范围" style"border-radius: 4px;" /><input id&qu…

RuoYi-Vue Swagger 上传文件接口

前言 RuoYi-Vue&#xff1a; v3.8.5swagger 1.6.2 &#xff08;https://github.com/swagger-api/swagger-core, https://gitee.com/mirrors/swagger-core&#xff09; Swagger 上传接口定义 ApiOperation(value "图片上传") PostMapping(value "/upload&qu…

SpringBoo集成MongoDB

一、集成简介 spring-data-mongodb提供了MongoTemplate与MongoRepository两种方式访问mongodb&#xff0c;MongoRepository操作简单&#xff0c;MongoTemplate操作灵活&#xff0c;我们在项目中可以灵活适用这两种方式操作mongodb&#xff0c;MongoRepository的缺点是不够灵活…

OpenMMLab-AI实战营第二期——相关3. RGB语义分割标注图像转为Gray格式的mask

文章目录 1. 转换代码1.1 查看原始图像1.2 转换1.3 cv::IMREAD_GRAYSCALE与CV_BGR2GRAY结果不一致1.3.1 现象描述1.3.2 原因1.3.3 推荐做法 1.4 CV_BGR2GRAY和CV_RGB2GRAY不一致 2. macOS上查看mask&#xff08;使用默认的预览&#xff09; 1. 转换代码 找到了一个语义分割的数…

rc表格卡方检验

一、案例介绍 某医院用三种穴位针刺治疗急性腰扭伤&#xff0c;现在想比较三种穴位针刺效果有无差别&#xff0c;结果汇总如下表&#xff1a; 二、问题分析 本案例想比较三种穴位针刺效果有无差别&#xff0c;可以使用RxC卡方检验进行分析。 通常情况下&#xff0c;共有三种…

uniapp项目 封装一个饼图组件 并且修改显示项的排列方式

需求如下: 真实数据渲染后的完成效果如下: 记录一下代码: <template><view><view style"height: 600rpx;"><l-echart ref"chart" finished"init"></l-echart></view></view> </template><…

【面试】一文知晓---拦截器和过滤器的区别

目录 背景关系图 拦截器和过滤器的区别实操1.过滤器1.1HttpServletRequestWrapper1.2 OncePerRequestFilter1.3 配置 2.拦截器2.1登录拦截2.2配置 3.监听器 三、注意1.静态资源问题2.登录拦截ajax重定向 总结 背景 关系图 然后具体执行流程如下&#xff1a; 拦截器和过滤器的区…

IDEA创建一个Servlet项目(tomcat10)

一、创建maven项目 org.apache.maven.archetypes:maven-archetype-webapp 二、增加Servlet依赖 tomcat9及以前依赖 <!--加入servlet依赖&#xff08;servlet的jar&#xff09;--><dependency><groupId>javax.servlet</groupId><artifactId>ja…

MoblieNet

论文信息 论文名称&#xff1a;MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications 论文地址&#xff1a;https://arxiv.org/abs/1704.04861 研究背景和研究意义 之前的网络都倾向于将网络做得又大又深&#xff0c;并且不考虑网络的速度&…

测试CefSharp.WinForms的基本用法

微信公众号“dotNET全栈开发”的文章《C#使用CefSharp内嵌网页-并给出C#与JS的交互示例》介绍了CefSharp的基本用法。CefSharp支持在.net程序中内置Chromium&#xff0c;它是Chromium Embedded Framework (CEF) 的轻量化封装。   CefSharp面向Winform、wpf等提供对应的NuGet包…

SpringBoot初始化接口CommandLineRunner

CommandLineRunner的使用 接口定义使用执行顺序使用 Order 注解实现Orderd接口排序总结 接口定义 Spring官方给出的接口定义 package org.springframework.boot;FunctionalInterface public interface CommandLineRunner {void run(String... args) throws Exception; }在 Sp…

卡方检验之多重比较

一、案例介绍 某医师研究物理疗法、药物治疗和外用膏药3种疗法治疗周围性面神经麻痹的疗效&#xff0c;通过整体卡方检验已经得知3种疗法有效率的差异有统计学意义&#xff08;χ221.0377&#xff0c;p0.000&#xff09;的结论。现在想进一步知道&#xff0c;具体是哪两种疗法…

Android后台应用开启前台服务---android8到android12梳理

1、Android 8.0 异常报错 在Android 8.0 系统中&#xff0c;处于后台的应用想要开启前台服务&#xff0c;必须满足两点&#xff1a; 在Activity中调用startForegroundService()方法所调起的Service必须执行startForeground(int id, Notification notification)方法&#xff0…

计算几何——gitf-wrapping算法

几何中的"gift-wrapping"算法&#xff0c;又称为"Jarvis算法"&#xff0c;是一种用于计算凸包(convex hull)的方法。下面我将为你解释一下该算法的步骤&#xff1a; 1. 找到具有最小x坐标的点P&#xff0c;我们将其作为凸包的起点。 2. 将P标记为当前点&a…

doker安装RabbitMQ以及用java连接

目录 doker安装&#xff1a; RabitMq安装&#xff1a; java链接 doker安装&#xff1a; 参考链接&#xff08;非常详细&#xff09;&#xff1a; docker安装以及部署_docker bu shuminio_春风与麋鹿的博客-CSDN博客 安装好后开启doker //启动docker服务 systemctl start do…

PromQL讲解与实战操作

PromQL讲解与实战操作 一、PromQL 介绍 PromQL&#xff08;Prometheus Query Language&#xff09;是 Prometheus 内置的数据查询语言&#xff0c;它能实现对事件序列数据的查 询、聚合、逻辑运算等。它并且被广泛应用在 Prometheus 的日常应用当中&#xff0c;包括对数据查询…

在Linux服务器上简单部署一个Python项目

一、在对应的服务器上检查有无Python环境 大部分Linux系统都是自带了Python环境的&#xff0c;查看是否具备Python的运行环境&#xff0c;可以通过命令Python --version 或者 Python3 --version 查看。如果你的项目需要Python3的环境&#xff0c;而系统又没有&#xff0c;则需要…

Linux 开启 swap 分区详细教程

故事背景&#xff1a; 哥们云机器内存资源不足&#xff0c;搞个 kafka eagle 监控&#xff0c;刚跑起来就卡死了&#xff0c;就很无语&#xff0c;哥们忙活&#xff0c;算了直接开搞&#xff0c;内存不够&#xff0c;硬盘来凑&#xff0c;拿着硬盘去做swap分区&#xff0c;也能…