【问题标题】:What am I missing in this code, does it look good and properly formatted?我在这段代码中遗漏了什么,它看起来不错并且格式正确吗?
【发布时间】:2023-03-31 21:55:01
【问题描述】:

编写一个输入文本文件的程序。程序应按字母顺序打印文件中的唯一单词。大写单词应优先于小写单词。例如,“Z”在“a”之前。

输入文件可以包含一个或多个句子,或者是多行单词列表。

“敏捷的棕狐跳过懒狗”

textFileName = input("Enter the input file name: ")
textFile = open(textFileName, 'r')
listOfWords = []   
while True:
        line = textFile.readline()
        if line == "":
            break
        else:
            words = line.split()
            for word in words:
                listOfWords.append(word)
                listOfWords.sort()
                uniqueListOfWords = []
                for count in range(len(listOfWords) -1):
                    if listOfWords[count]!= listOfWords[count + 1]:
                        uniqueListOfWords.append(listOfWords[count])
                        uniqueListOfWords.append(listOfWords[len(listOfWords) -1])
                        print(word)

【问题讨论】:

    标签: input uppercase


    【解决方案1】:

    首先,在读取文件时,您可以做一些更简单的事情来获取每个连续单词的列表。即,

    fname = input("Enter the input filename: ") # get the filename
    
    f = open(fname, 'r') # open the file
    contents = f.read() # read the file contents
    f.close() # close the file (you didn't before)
    
    all_words = contents.split(' ') # get every word in original order
    

    至于排序,这似乎很好。但是,在创建包含所有唯一值的唯一单词列表时,您可以使用一个简单的技巧:unique = list(set(sorted(all_words)))。简而言之,set 类型的每个唯一值只能有 1 个。因此,如果您将其转换为集合,然后再转换回列表,您将获得按字母顺序排列的所有唯一值的列表。 sorted 函数返回列表的排序版本,而 list.sort 对列表进行排序,并且不返回任何内容。

    最后,如果还没有关闭文件,请记住始终关闭它。

    最重要的是,考虑注释一些代码以帮助未来的读者理解它的每个部分的含义,并不时包含一些空格(即空格、换行符)以分隔不同的部分并使其看起来只是一个好一点。不过,这就是我能想到的。

    编辑: 我意识到我应该包含修改后代码的摘要版本,所以这里是:

    fname = input("Enter the input filename: ") # get the filename
    
    f = open(fname, 'r') # open the file
    contents = f.read() # read the file contents
    f.close()
    
    all_words = contents.split(' ') # get every word in original order
    unique_words = list( set( sorted( all_words ) ) ) # sort the words, then convert it to a set type which can only contain 1 of each unique value, and convert it back to a list
    
    for word in unique_words:
        print word # print every unique word
    

    【讨论】:

      【解决方案2】:

      我希望我按照你提到的那样进行了编辑,但是在对我的代码进行了一些更改后,这对我有用。

      def sorted_unique_file(input_filename): 
      
          input_file = open(input_filename, 'r') #open file in read mode
      
          file_contents = input_file.read() #read text from file
      
          input_file.close()
          
          unique = []
      
          word_list = file_contents.split()
      
          for word in word_list:
      
              if word not in unique:
      
                  unique.append(word) #add unique words into list
      
          print("unique words in alphabetical order are: ")
      
          print("\n".join(sorted(list(set(unique))))) #sort the words using set operation
      
      def main():
      
          filename = input("Enter the input file name: ") #read the file name
      
          sorted_unique_file(filename) #call the function to print unique words in alphabetical order
      
      if __name__ == "__main__":
      
          main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多