首页 > 解决方案 > 如何解决 AttributeError:'list' 对象在 python 中没有属性'keys'

问题描述

    if w in data:
        return data[w]
    # for getting close matches of word
    elif len(get_close_matches(w, data.keys())) > 0:            
        yn = input("Did you mean % s instead? Enter Y if yes, or N if no: " % get_close_matches(w, data.keys())[0])
        yn = yn.lower()
        if yn == "y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "n":
            return "The word doesn't exist. Please double check it."
        else:
            return "We didn't understand your entry."
    else:
        return "The word doesn't exist. Please double check it."

标签: pythonpython-3.xfunctiondictionarymodule

解决方案


data代码中的对象不是字典。这是一个列表,列表没有键。您需要将data对象实现为字典。

Python中列表和字典的区别


推荐阅读