3 回答

TA貢獻1829條經驗 獲得超4個贊
你可以functools.lru_cache用來做緩存。我接受一個maxsize參數來控制它緩存的數量:
from functools import lru_cache
@lru_cache(maxsize=2)
def test(n):
print("calling function")
return n * 2
print(test(2))
print(test(2))
print(test(3))
print(test(3))
print(test(4))
print(test(4))
print(test(2))
結果:
調用函數
4
4
調用函數
6
6
調用函數
8
8
調用函數
4

TA貢獻1815條經驗 獲得超13個贊
解決方案
您可以使用以下方法修復您擁有的代碼OrderedDict:
from collections import OrderedDict
def memoize(f, k):
cache = OrderedDict()
def mem_f(*args):
if args in cache:
return cache[args]
result = f(*args)
if len(cache) >= k:
cache.popitem(last=False)
cache[args]= result
return result
return mem_f,cache
測試一下
def mysum(a, b):
return a + b
mysum_cached,cache = memoize(mysum, 10)
for i in range(100)
mysum_cached(i, i)
print(cache)
輸出:
OrderedDict([((90, 90), 180), ((91, 91), 182), ((92, 92), 184), ((93, 93), 186), ((94, 94), 188), ((95, 95), 190), ((96, 96), 192), ((97, 97), 194), ((98, 98), 196), ((99, 99), 198)])
此版本memoize可能適用于您自己的代碼。但是,對于生產代碼(即其他人必須依賴的代碼),您可能應該使用functools.lru_cacheMark Meyer 建議的標準庫函數 ( )。

TA貢獻1906條經驗 獲得超3個贊
擴展 Mark Meyer 的絕妙建議,以下是解決方案的使用方式lru_cache和問題的術語:
from functools import lru_cache
def memoize(f, k):
mem_f = lru_cache(maxsize=k)(f)
return mem_f
def multiply(a, b):
print("Called with {}, {}".format(a, b))
return a * b
def main():
memo_multiply = memoize(multiply, 2)
print("Answer: {}".format(memo_multiply(3, 4)))
print("Answer: {}".format(memo_multiply(3, 4)))
print("Answer: {}".format(memo_multiply(3, 7)))
print("Answer: {}".format(memo_multiply(3, 8)))
if __name__ == "__main__":
main()
結果:
Called with 3, 4
Answer: 12
Answer: 12
Called with 3, 7
Answer: 21
Called with 3, 8
Answer: 24
添加回答
舉報