【问题标题】:How to slove AttributeError: 'list' object has no attribute 'keys' in python [closed]如何解决 AttributeError:'list' 对象在 python 中没有属性'keys' [关闭]
【发布时间】:2021-11-08 03:30:39
【问题描述】:
    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."

【问题讨论】:

    标签: python python-3.x function dictionary module


    【解决方案1】:

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

    Difference between List and Dictionary in Python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 2021-08-09
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2023-02-15
      相关资源
      最近更新 更多