← 返回题库
初级

统计各设备缓存命中率

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd
    cache_csv = open_url("https://data.zuihe.com/dbd/ms-iot/state_07/cache_log.csv").read()
    cache = pd.read_csv(StringIO(cache_csv))
    reads = cache[cache['operation']=='read'].copy()
    reads['device_id'] = reads['cache_key'].str.split(':').str[1]
    print("Cache hit rate by device:")
    for did, grp in reads.groupby('device_id'):
        hr = grp['hit'].mean()
        print("  " + did + ": " + str(round(hr*100,1)) + "% (" + str(int(grp['hit'].sum())) + "/" + str(len(grp)) + ")")
    total_hr = reads['hit'].mean()
    print("Overall hit rate: " + str(round(total_hr*100,1)) + "%")

示例

输入
solve()
期望输出
Cache hit rate by device:
  DEV001: 75.0% (3/4)
  DEV002: 0.0% (0/1)
  DEV004: 100.0% (5/5)
  DEV005: 50.0% (1/2)
  DEV007: 0.0% (0/1)
  DEV010: 100.0% (3/3)
Overall hit rate: 75.0%
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码