简体   繁体   English

检查涉及Python中any()函数的条件时键入Error

[英]Type Error when checking a condition involving the any() function in Python

First question on StackExchange... I've been learning Python to do some text analysis for a few months now, running Python 3.5 on a Mac, and I've got a problem with some code that looks like this: 关于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

When the loop starts, condition_1 and condition_2 are False , so I would expect the first iteration of the loop to get to the fourth case and do that. 当循环开始时, condition_1condition_2False ,因此我希望循环的第一次迭代达到第四种情况并执行该操作。 But instead when it checks the third case it throws the following error message: 但是,当它检查第三种情况时,它会抛出以下错误消息:

TypeError: distance expected two Strings or two Unicodes

If I go to that line in the debugger and check the type of w and the elements of the keywords list, they're all str . 如果我在调试器中转到该行并检查w的类型和keywords列表的元素,它们都是str If I try just running the problematic command in the debugger, I get a NameError: 如果尝试在调试器中运行有问题的命令,则会收到NameError:

(Pdb) any(distance(w, k) < 2 for k in keywords)
*** NameError: name 'w' is not defined

If instead I put in the literal assigned to w everything is fine: 相反,如果我输入分配给w的文字,那么一切都很好:

(Pdb) any(distance('some', k) < 2 for k in keywords)
True

Which makes me think it has something to do with how the generator function treats namespaces, but I'm not sure. 这使我认为它与生成器函数如何处理名称空间有关,但我不确定。 I've tried substituting a builtin function for distance(w, k) , eg len(w + k) , just to check, and I get the same set of errors. 我试图用内置函数代替distance(w, k) ,例如len(w + k) ,只是为了检查,并且得到了同样的错误集。 I find the error particularly confusing as there appears to be no problem in checking the first two cases. 我发现该错误特别令人困惑,因为检查前两种情况似乎没有问题。

I could come up with a workaround, but I'd like to know what's actually going on here. 我可以想出一种解决方法,但是我想知道这里到底发生了什么。 Google and StackExchange have so far brought me no joy on this topic. 到目前为止,Google和StackExchange在这个话题上都给我带来了喜悦。 Any thoughts or suggestions would be gratefully received. 任何想法或建议将不胜感激。

you could make this a lot simpler by reformatting your if statements inside the loop and checking the type of W as follows: 您可以通过在循环内重新格式化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

I'm guessing your are getting these errors running with a different string than the example. 我猜您正在使用与示例不同的字符串运行这些错误。 Have you checked it actually has a value before you split it ? 在拆分之前,您是否检查过它实际上是否有值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM