为什么在第13行出现语法错误?

时间:2020-07-29 02:54:49

标签: python

def calculate_frequencies(file_contents):
    # Here is a list of punctuations and uninteresting words you can use to process your text
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
    dict = {}
    x = [file_contents.lower().split()]
    new_list = []
    for word.isalpha in x:
        if word.isalpha not uninteresting_words:
            new_list.append(word.isalpha)
    
    for word in new_list:
        if word not in dict.keys():
            dict[word] = new_list.count(word)

    
    #wordcloud
    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies()
    return cloud.to_array()
File "<ipython-input-11-ea9576de222d>", line 13
    if word.isalpha not uninteresting_words:
                                          ^
SyntaxError: invalid syntax

我收到了意外的语法错误,请帮忙。

2 个答案:

答案 0 :(得分:2)

您需要将in放在第13行:

def calculate_frequencies(file_contents):
# Here is a list of punctuations and uninteresting words you can use to process your text
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
"we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
"their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
"have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
"all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
dict = {}
x = [file_contents.lower().split()]
new_list = []
for word.isalpha in x:
    // Here....
    if word.isalpha not in uninteresting_words:
        new_list.append(word.isalpha)

for word in new_list:
    if word not in dict.keys():
        dict[word] = new_list.count(word)


#wordcloud
cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies()
return cloud.to_array()

答案 1 :(得分:0)

isalpha是一个返回布尔值(True \ False)

的函数
相关问题