如何消除python字典错误消息

2024-06-12 10:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想看看这个词是否存在于词典中,或者没有使用PyDictionary库。我有以下代码,但我可以看到一些错误,如“列表索引超出范围”,我无法理解。我是python新手

def fst_wrd(exmp): 
    from PyDictionary import PyDictionary
    dictionary=PyDictionary()
 try: 
      var = (dictionary.meaning(exmp)) 
except: 
      if var == None: 
      return(False) 
else: 
      return (True) 
for exmp in my_list: 
      result = fst_wrd(exmp) 
      if result == True: 
        print(exmp, "is dict")
      else:
        print(exmp, "not dict")

假设我有一个列表mylist=[“on”,“carrot”,“hello”,“tunc”]。我得到的错误是list:index超出范围


Tags: true列表dictionaryreturnifvar错误result
1条回答
网友
1楼 · 发布于 2024-06-12 10:44:18

尝试使用meaning(word, disable_errors=True)禁用错误消息。您可以尝试以下代码:

def fst_wrd(exmp): 
    from PyDictionary import PyDictionary 
    dictionary = PyDictionary() 
    try: 
       var = dictionary.meaning(exmp, disable_errors=True) 
    except: 
       return False 
    else: 
       return True

相关问题 更多 >