返回字典.txt中的单词列表



我是Python的初学者。我乞求了解函数和文件。我需要编写一个代码,该代码将使用函数返回dictionary.txt中所有单词的列表,但我似乎无法弄清楚。

当前代码

def get_dictionary_wordlist(dic):  
     dic = open("dictionary.txt", "r")
     for word in dic:
         return word

你必须做一个列表,在那里添加单词,然后你返回它。

def get_dictionary_wordlist(dic):  
     words = [] # Make a list
     dic = open("dictionary.txt", "r")
     for word in dic:
         words.append() # Add words to the list
     return words

这应该有效:

dictionary = ('Italy', 'pizza'), ('Germany', 'sauerkraut'), ('Spain', 'paella'), ('USA', 'Hamburger') # Opening the dictionary
def listWords(dic): # Creating the function
    y = dict(dic) # Making sure dic is a dic
    x = [] #creating the list
    for key in dic: # Iterating through every item in dic
        x.append(key) # Adding the item to the end
    return x #returning the results
print listWords(dictionary) #printing the results for the original text, dictionary

最新更新