Python-Dictionary函数只有一个字典条目

Python-Dictionary函数只有一个字典条目,python,dictionary,Python,Dictionary,抱歉,如果这是一个愚蠢的问题,但我是python新手。我有一段代码,打开一个文本阅读它,创建一个单词列表,然后根据该列表创建一个每个单词的字典,并计算它在单词列表中出现的次数。这段代码工作正常,并且很好地打印了字典。但是,当我将它放入一个函数并调用该函数时,它返回字典,但只返回一个条目。任何想法,为什么,任何帮助都是非常感谢的 def createDict(): wordlist = [] with open('superman.txt','r', encoding="ut

抱歉,如果这是一个愚蠢的问题,但我是python新手。我有一段代码,打开一个文本阅读它,创建一个单词列表,然后根据该列表创建一个每个单词的字典,并计算它在单词列表中出现的次数。这段代码工作正常,并且很好地打印了字典。但是,当我将它放入一个函数并调用该函数时,它返回字典,但只返回一个条目。任何想法,为什么,任何帮助都是非常感谢的

def createDict():

    wordlist = []


    with open('superman.txt','r', encoding="utf8") as superman:
                for line in superman:
                    for word in line.split():
                        wordlist.append(word)
                   #print(word)    



    table = str.maketrans("!#$%&()*+, ./:;<=>?@[\]^_`{|}~0123456789'“”-''—", 47*' ' )


    lenght = len(wordlist)
    i = 0


    while i < lenght:
            wordlist[i] = wordlist[i].translate(table)
            wordlist[i] = wordlist[i].lower()
            wordlist[i] = wordlist[i].strip()
            i += 1


    wordlist = list(filter(str.strip, wordlist))

    word_dict = {}

    for item in wordlist:
             if item in word_dict.keys():
                word_dict[item] += 1
    else:
                word_dict[item] = 1

    return(word_dict)
def createDict():
单词表=[]
用open('superman.txt','r',encoding=“utf8”)作为超人:
对于《超人》中的台词:
对于第行中的单词。拆分():
追加(word)
#打印(word)
table=str.maketrans(“!\$%&()*+,./:;?@[\]^ `{124;}~ 0123456789'”—“-”,47*”)
长度=长度(字表)
i=0
而我
尝试在函数外部初始化字典,然后在函数内部使用global。字典中的这一项是最后一次迭代吗?

修复迭代单词列表时的缩进。应改为:

对于单词列表中的项目:
如果word_dict.keys()中的项:
单词dict[项目]+=1
其他:
单词dict[项目]=1

这似乎是缩进和空白问题。确保函数末尾附近的if和else语句处于同一级别

下面是我在正确级别使用缩进的代码。此外,还对思维过程进行了说明

def createDict():

    wordlist = []


    with open('superman.txt','r', encoding="utf8") as superman:
                for line in superman:
                    for word in line.split():
                        wordlist.append(word)
                   #print(word)    



    table = str.maketrans("!#$%&()*+, ./:;<=>?@[\]^_`{|}~0123456789'“”-''—", 47*' ' )


    lenght = len(wordlist)
    i = 0


    while i < lenght:
            wordlist[i] = wordlist[i].translate(table)
            wordlist[i] = wordlist[i].lower()
            wordlist[i] = wordlist[i].strip()
            i += 1


    wordlist = list(filter(str.strip, wordlist))
    # print(len(wordlist)) # check to see if wordlist is fine. Indeed it is
    word_dict = {}

    for item in wordlist:
        # for dictionaries don't worry about using dict.keys()
        # method. You can use a shorter if [value] in [dict] conditional
        # The issue in your code was the whitespace and indentation
        # of the else statement. 
        # Please make sure that if and else are at the same indentation levels
        # Python reads by indentation and whitespace because it
        # doeesn't use curly brackets like other languages like javascript
        if item in word_dict:
            word_dict[item] += 1
        else:
            word_dict[item] = 1

    return word_dict # print here too

def createDict():
单词表=[]
打开(“superman.txt”、“r”、encoding=“utf8”)为超人:
对于《超人》中的台词:
对于第行中的单词。拆分():
追加(word)
#打印(word)
table=str.maketrans(“!\$%&()*+,./:;?@[\]^ `{124;}~ 0123456789'”—“-”,47*”)
长度=长度(字表)
i=0
而我

如果你有任何问题,请告诉我。干杯

缩进成功了,非常感谢韦恩,你是我本周的英雄!谢谢Keven,非常感谢!谢谢皮特,非常感谢!