我正在尋找使代碼更高效的方法(運行時和內存復雜性) 我應該使用像 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....#####################################################################################
添加回答
舉報
0/150
提交
取消