← 返回题库
中级

实现字段过滤(fields参数)

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd, json
    devs_csv = open_url(____).read()
    devs = pd.read_csv(StringIO(____))
    def get_devices_with_fields(____):
        if not fields: return df.to_dict(____)
        valid = [f for f in fields if f in df.columns]
        return df[valid].to_dict(____)
    test_cases = [
        ['____','____','____'],
        ['____','____','____'],
        None,
    ]
    for fields in test_cases:
        result = get_devices_with_fields(devs.head(____), fields)
        print(____)

示例

输入
solve()
期望输出
fields=['device_id', 'device_name', 'status']: keys=['device_id', 'device_name', 'status']
fields=['device_id', 'device_type', 'location_id']: keys=['device_id', 'device_type', 'location_id']
fields=None: keys=['id', 'device_id', 'device_name', 'device_type', 'location_id', 'manufacturer', 'firmware_version', 'status', 'registered_at', 'last_seen_at']
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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