为了账号安全,请及时绑定邮箱和手机立即绑定

如何优化 json 数据对象中 10 个最常用词的检索?

如何优化 json 数据对象中 10 个最常用词的检索?

忽然笑 2021-09-25 21:53:14
我正在寻找使代码更高效的方法(运行时和内存复杂性) 我应该使用像 Max-Heap 这样的东西吗?由于字符串连接或字典排序不就地或其他原因导致的性能不佳? 编辑:我将字典/地图对象替换为在所有检索到的名称列表上应用 Counter 方法(有重复)最小请求: 脚本应该花费少于 30 秒 当前运行时间:它需要 54 秒   # Try to implement the program efficiently (running the script should take less then 30 seconds)import requests# Requests is an elegant and simple HTTP library for Python, built for human beings.# Requests is the only Non-GMO HTTP library for Python, safe for human consumption.# Requests is not a built in module (does not come with the default python installation), so you will have to install it:# http://docs.python-requests.org/en/v2.9.1/# installing it for pyCharm is not so easy and takes a lot of troubleshooting (problems with pip's main version)# use conda/pip install requests insteadimport json# dict subclass for counting hashable objectsfrom collections import Counter#import heapqimport datetimeurl = 'https://api.namefake.com'# a "global" list object. TODO: try to make it "static" (local to the file)words = []###################################################################################### Calls the site http://www.namefake.com  100 times and retrieves random names# Examples for the format of the names from this site:# Dr. Willis Lang IV# Lily Purdy Jr.# Dameon Bogisich# Ms. Zora Padberg V# Luther Krajcik Sr.# Prof. Helmer Schaden            etc....#####################################################################################
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 170 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号