python实践项目三:将列表添加到字典
1、创建一个字典,其中键是字符串,描述一个物品,值是一个整型值,说明有多少该物品。例如,字典值{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}意味着有 1 条绳索、 6 个火把、 42 枚金币等。
2、写一个名为 displayInventory()的函数,显示出字典中所有物品及其数量,并统计出总数量
3、写一个名为 addToInventory(inventory, addedItems)的函数, 其中 inventory 参数是一个字典, 存储物品清单, addedItems 参数是一个列表,存储需要更新的物品。addToInventory()函数应该返回一个字典,表示更新过后的物品清单。
代码一:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#打印字典
def displayInventory(inventory):
print 'Inventory:'
item_total=0
for k,v in inventory.items():
print str(v)+' '+k
item_total+=v
print 'Total number of items:'+str(item_total)
#列表添加到字典
def addToInventory(inventory,addItems):
for k in addItems:
if k in inventory.keys():
inventory[k]+=1
else:
inventory[k]=1
return inventory #初始字典
inv={'gold coin':42,'rope':1}
#需要添加的列表
dragonLoot=['gold coin','dagger','gold coin','gold coin','ruby']
#将列表添加到字典
inv=addToInventory(inv,dragonLoot)
#显示更新后的字典
displayInventory(inv)
显示结果:

代码二(实现同样功能):
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def displayInventory(inven):
print "Inventory:"
item_total=0
for k,v in inven.items():
print str(v)+" "+k
item_total+=v
print "Total number of the items: "+str(item_total) def addListToInventory(inven,addedItems):
for i in range(len(addedItems)):
if addedItems[i] in inven.keys():
inven[addedItems[i]]+=1
else:
inven.setdefault(addedItems[i],1)
return inv
inv={'gold coin':42,'rope':1}
addedList=['gold coin','dagger','gold coin','gold coin','ruby']
inv=addListToInventory(inv,addedList)
displayInventory(inv)
运行结果:

python实践项目三:将列表添加到字典的更多相关文章
- python实践项目二:列表转字符串
将列表各元素转换为字符串并以规定形式返回. 假定有下面这样的列表:spam = ['apples', 'bananas', 'tofu', 'cats'],将其转换成字符串:'apples, bana ...
- android 实践项目三
android 实践项目三 本周我主要完成的任务是将代码进行整合,然后实现百度地图的定位与搜索功能.在这次实现的 图形界面如下: 在本周的工作中主要的实现出来定位与收索的功能,在地图中能实现了定位,显 ...
- python 实践项目
项目一:让用户输入圆的半径,告诉用户圆的面积 思路: 1.首先需要让用户输入一个字符串,即圆的半径 2.判断用户输入的字符串是否为数字 isalpha 3.求圆的面积需要调用到math模块,所以要导 ...
- python入门(三)列表、元组、range()、字典
列表(list) 列表简介:列表(list)是处理一组有序项目的数据结构.用方括号[]表示.可以进行添加,删除,替换,搜索操作.是可变的数据类型.列表可以嵌套和支持索引. name=[12," ...
- Python实践项目2
#南昌理工学院人工智能学院实验室WORKSHOP实践项目 import time import random SCRIPT_NPC_SCHOOL_SISTER = ['你好!', '你好!', '你是 ...
- python实践项目1
python #南昌理工学院人工智能学院实验室 WORKSHOP 实践项目 import time print('welcome to our WORKSHOP') print('.......... ...
- 第4.4节 Python解析与推导:列表解析、字典解析、集合解析
一. 引言 经过前几个章节的介绍,终于把与列表解析的前置内容介绍完了,本节老猿将列表解析.字典解析.集合解析进行统一的介绍. 前面章节老猿好几次说到了要介绍列表解析,但老猿认为涉及知识层面比较多 ...
- Python:从入门到实践--第三章--列表简介--练习
#1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...
- python基础(三)--列表、元组、字典
一.列表: 有序序列,支持索引.切片.循环(for,while) 元素可以被修改: 元素可以是任何数据类型(数字,字符串,列表,布尔值...),可以嵌套: ##增 1.append(object) ...
随机推荐
- test20190905 ChiTongZ
100+22+90=212.前两道题不错,但T3 没什么意义. 围观刘老爷超强 T1 解法. ChiTongZ的水题赛 [题目简介] 我本可以容忍黑暗,如果我不曾见过太阳. 考试内容略有超纲,不超纲的 ...
- 记录一次编译安装Pg_rman缺少依赖包的问题
系统版本:CentOS版本6.10(最终版) pg_rman:https://github.com/ossc-db/pg_rman -bash-4.1$ makegcc -Wall -Wmissing ...
- Open Source Isn't A Business Model, It's A Market Strategy
https://www.forbes.com/sites/quora/2017/09/29/open-source-isnt-a-business-model-its-a-market-strateg ...
- thinkphp 打印sql语句方法
thinkphp 打印数据use think\Db; 引用db 需要查询的sql语句连锁查询 db()->table('business_order')->alias('o')->j ...
- SpringBoot第三节(thymeleaf的配置与SpringBoot注解大全)
Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.所以这里介绍一下Springboot使用Thymeleaf的实例以及遇到的问题. 1.配置与使用 1.1:在applica ...
- Mysql注入绕过安全狗
转载请加原文链接:https://www.cnblogs.com/Yang34/p/12055052.html 微信公众号:信Yang安全.同步更新,欢迎关注.文末有二维码. 正好最近在搞注入,昨天现 ...
- BZOJ 3166: [Heoi2013]Alo 链表+可持久化trie
链表这个东西非常好用啊 ~ code: #include <bits/stdc++.h> #define N 50010 #define inf 2000400000 #define se ...
- learning java 实例序列化
对Person类实例进行序例化及反序例化: Person.java public class Person implements java.io.Serializable { private Stri ...
- Mscordacwks.dll/SOS.dll 调试归档
找到个好东西 为什么要归档 此存档提供帮助,并可能提供对以下问题的答案 是否可以使WinDBG在符号存储中找到mscordacwks.dll?, Windbg需要不同版本的mscordacwks.dl ...
- WinDbg常用命令系列---.load, .loadby (Load Extension DLL)
.load, .loadby (Load Extension DLL) 简介 .load和.loadby命令将新的扩展DLL加载到调试器中. 使用形式 .load DLLName !DLLName.l ...