网站登记备案表,电商平台建设方案,个人网站备案要求,wordpress没有首页Python “字典” 实战案例#xff1a;5个项目开发实例
内容摘要
本文包括 5 个使用 Python 字典的综合应用实例。具体是#xff1a;
电影推荐系统配置文件解析器选票统计与排序电话黄页管理系统缓存系统#xff08;LRU 缓存#xff09;
以上每一个实例均有完整的程序代…Python “字典” 实战案例5个项目开发实例
内容摘要
本文包括 5 个使用 Python 字典的综合应用实例。具体是
电影推荐系统配置文件解析器选票统计与排序电话黄页管理系统缓存系统LRU 缓存
以上每一个实例均有完整的程序代码以及测试案例帮助读者快速理解和掌握项目开发相关技术和技巧。 1. 电影推荐系统
功能描述 实现一个简单的电影推荐系统根据用户的喜好推荐电影。
代码实现
# 电影推荐系统
class MovieRecommender:def __init__(self):self.movies {Action: [The Dark Knight, Inception, Mad Max: Fury Road],Comedy: [The Hangover, Superbad, Step Brothers],Drama: [The Shawshank Redemption, Forrest Gump, The Godfather]}def recommend(self, genre):return self.movies.get(genre, 未找到该类型的电影)# 测试
recommender MovieRecommender()
print(动作电影推荐:, recommender.recommend(Action))
print(科幻电影推荐:, recommender.recommend(Sci-Fi)) # 未找到2. 配置文件解析器
功能描述 实现一个配置文件解析器将配置文件中的键值对解析为字典。
代码实现
# 配置文件解析器
def config_parser(file_path):config {}with open(file_path, r) as file:for line in file:line line.strip()if line and not line.startswith(#): # 忽略空行和注释key, value line.split(, 1)config[key.strip()] value.strip()return config# 测试
file_path config.txt # 假设配置文件内容如下
# host localhost
# port 8080
# debug True
config config_parser(file_path)
print(config) # 输出: {host: localhost, port: 8080, debug: True}3. 选票统计与排序
功能描述
实现按姓名统计参选人得票数并按得票高低排序。 代码实现
# 统计选票并按得票数排序
def count_votes(votes):# 统计得票数vote_count {}for candidate in votes:if candidate in vote_count:vote_count[candidate] 1else:vote_count[candidate] 1# 按得票数从高到低排序sorted_votes sorted(vote_count.items(), keylambda x: x[1], reverseTrue)# 将排序结果转换为字典sorted_vote_dict dict(sorted_votes)return sorted_vote_dict# 测试案例
votes [Trump, Biden, Roger, Trump, Trump, David, Biden, Trump, Biden, Roger
]# 统计并排序
result count_votes(votes)
print(选票统计结果按得票数从高到低排序)
for candidate, count in result.items():print(f{candidate}: {count} 票)输出结果
选票统计结果按得票数从高到低排序
Trump: 4 票
Biden: 3 票
Roger: 2 票
David: 1 票 代码解析 统计得票数 使用字典 vote_count 统计每个参选人的得票数。遍历选票列表 votes如果参选人已经在字典中则得票数加 1否则初始化得票数为 1。 按得票数排序 使用 sorted() 函数对字典的键值对进行排序。keylambda x: x[1] 表示按值得票数排序。reverseTrue 表示从高到低排序。 转换为字典 排序后的结果是一个列表每个元素是一个元组 (参选人, 得票数)。使用 dict() 将排序后的列表转换回字典。 输出结果 遍历排序后的字典输出每个参选人的得票数。 4. 电话黄页管理系统
功能描述 实现一个电话簿管理系统支持添加联系人、查找联系人、删除联系人和显示所有联系人。
代码实现
# 电话簿管理系统
class PhoneBookManager:def __init__(self):self.contacts {}def add_contact(self, name, number):if name in self.contacts:print(f联系人 {name} 已存在)else:self.contacts[name] numberprint(f联系人 {name} 添加成功)def find_contact(self, name):return self.contacts.get(name, 联系人不存在)def delete_contact(self, name):if name in self.contacts:del self.contacts[name]print(f联系人 {name} 已删除)else:print(f联系人 {name} 不存在)def show_all(self):if not self.contacts:print(电话簿为空)else:print(所有联系人)for name, number in self.contacts.items():print(f{name}: {number})# 测试
phone_book PhoneBookManager()
phone_book.add_contact(Alice, 123456789)
phone_book.add_contact(Bob, 987654321)
phone_book.delete_contact(Bob)
phone_book.show_all()5. 缓存系统LRU 缓存
功能描述 实现一个 LRU最近最少使用缓存系统支持添加缓存、获取缓存和限制缓存大小。
代码实现
from collections import OrderedDict# LRU 缓存系统
class LRUCache:def __init__(self, capacity):self.cache OrderedDict()self.capacity capacitydef get(self, key):if key not in self.cache:return -1else:self.cache.move_to_end(key)return self.cache[key]def put(self, key, value):if key in self.cache:self.cache.move_to_end(key)self.cache[key] valueif len(self.cache) self.capacity:self.cache.popitem(lastFalse)# 测试
cache LRUCache(2)
cache.put(1, value1)
cache.put(2, value2)
print(cache.get(1)) # 输出: value1
cache.put(3, value3) # 淘汰 key 2
print(cache.get(2)) # 输出: -1总结
以上 5 个项目涵盖了字典在电影推荐、配置文件解析、选票统计排序、缓存系统等方面的典型应用。通过这些项目可以深入理解字典的灵活性和强大功能可以进一步巩固字典的使用技巧并掌握其在实际开发中的多样化应用。 文章转载自: http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.nchsz.cn.gov.cn.nchsz.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.qbfkz.cn.gov.cn.qbfkz.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.qpqb.cn.gov.cn.qpqb.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn