2017-12-03 191 views
0

我在我的代码,这个错误,我不知道如何固定错误类型错误:“海峡”对象不是可调用的蟒蛇

import nltk 
from nltk.util import ngrams 


def word_grams(words, min=1, max=4): 
    s = [] 
    for n in range(min, max): 
     for ngram in ngrams(words, n): 
      s.append(' '.join(str(i) for i in ngram)) 
    return s 

print word_grams('one two three four'.split(' ')) 

erorr在

s.append(' '.join(str(i) for i in ngram)) 

类型错误:“海峡'object is callable

+3

较浅的例子你定义使用'str'作为变量名的字符串? – abccd

+2

你在哪里定义了一个名为'str'的​​变量? –

回答

2

您发布的代码是正确的,并且可以与python 2.7和3.6一起使用(对于3.6,您必须将括号括在print语句中)。但是,代码原样有3个空格缩进,应该固定为4个空格。

这里如何重现你的错误

s = [] 
str = 'overload str with string' 
# The str below is a string and not function, hence the error 
s.append(' '.join(str(x) for x in ['a', 'b', 'c'])) 
print(s) 

Traceback (most recent call last): 
    File "python", line 4, in <module> 
    File "python", line 4, in <genexpr> 
TypeError: 'str' object is not callable 

必须有地方在那里你重新定义海峡内置运营商为STR值像上面的例子。

这里同样的问题

a = 'foo' 
a() 
Traceback (most recent call last): 
    File "python", line 2, in <module> 
TypeError: 'str' object is not callable 
1

我通过运行你的代码得到输出。

O/P: 
['one', 'two', 'three', 'four', 'one two', 'two three', 'three four', 'one two three', 'two three four'] 

我猜错误不会来。这是你期望的吗?

相关问题