Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中检查涉及any()函数的条件时出现类型错误_Python_Generator - Fatal编程技术网

在Python中检查涉及any()函数的条件时出现类型错误

在Python中检查涉及any()函数的条件时出现类型错误,python,generator,Python,Generator,关于StackExchange的第一个问题。。。几个月来,我一直在学习Python来进行一些文本分析,在Mac上运行Python 3.5,我发现一些代码有问题,如下所示: from Levenshtein import distance keywords = ['some', 'list'] line = 'some long string.' for w in line.split(): if condition_1 and not any(distance(w, k) <

关于StackExchange的第一个问题。。。几个月来,我一直在学习Python来进行一些文本分析,在Mac上运行Python 3.5,我发现一些代码有问题,如下所示:

from Levenshtein import distance
keywords = ['some', 'list']
line = 'some long string.'

for w in line.split():

    if condition_1 and not any(distance(w, k) < 2 for k in keywords):
        do_something

    elif condition_1 and any(distance(w, k) < 2 for k in keywords):
        do_something_else

    elif condition_2 and not any(distance(w, k) < 2 for k in keywords):
        do_a_third_thing

    else:
        do_somthing_completely_different
如果我转到调试器中的那一行,检查
w
的类型和
关键字列表中的元素,它们都是
str
。如果尝试在调试器中运行有问题的命令,则会出现NameError:

(Pdb) any(distance(w, k) < 2 for k in keywords)
*** NameError: name 'w' is not defined
这让我觉得它与生成器函数如何处理名称空间有关,但我不确定。我试着用一个内置函数替换距离(w,k)
,例如
len(w+k)
,只是为了检查,我得到了相同的错误集。我发现这个错误特别令人困惑,因为在检查前两个案例时似乎没有问题


我可以想出一个解决办法,但我想知道这里到底发生了什么。到目前为止,谷歌和StackExchange在这个话题上没有给我带来任何乐趣。如果您有任何想法或建议,我们将不胜感激。

您可以通过在循环中重新设置if语句的格式并检查W的类型来简化此过程,如下所示:

print(" W contains '{}' and is a {} ".format(w, type(w)))
assert w,str

if condition_1:
    result = any(distance(w, k) < 2 for k in keywords):
    print(" Result contains '{}' and is a {} ".format(result,type(result)  

    if result:    
        do_something
    else:
        do_something_else

elif condition_2:
    if not any(distance(w,k) < 2 for k in keywords):
        do_a_third_thing

else:
     do_something_completely_different
print(“W包含{}并且是{}”。格式(W,type(W)))
断言w,str
如果条件_1:
结果=任意(关键字中k的距离(w,k)<2):
打印(“结果包含{}并且是{}”。格式(结果,类型(结果)
如果结果为:
做点什么
其他:
你还有别的事吗
elif条件_2:
如果没有(关键字中k的距离(w,k)<2):
做第三件事
其他:
做些完全不同的事吗

我猜您得到的这些错误是使用与示例不同的字符串运行的。您在拆分它之前检查过它是否确实有值吗?

是的,我的代码中的字符串比我给出的示例中的字符串长得多。我检查过它是否有值,当我在得到错误的行插入断点并检查时在我的命名空间中,变量w实际上是一个字符串。
(Pdb) any(distance('some', k) < 2 for k in keywords)
True
print(" W contains '{}' and is a {} ".format(w, type(w)))
assert w,str

if condition_1:
    result = any(distance(w, k) < 2 for k in keywords):
    print(" Result contains '{}' and is a {} ".format(result,type(result)  

    if result:    
        do_something
    else:
        do_something_else

elif condition_2:
    if not any(distance(w,k) < 2 for k in keywords):
        do_a_third_thing

else:
     do_something_completely_different