【问题标题】:Python's alphabetical sort file errorPython的字母排序文件错误
【发布时间】:2017-07-04 11:07:09
【问题描述】:

我是 python 新手,这是我的问题: 打开文件 romeo.txt 并逐行读取。对于每一行,使用 split() 方法将该行拆分为一个单词列表。该程序应该建立一个单词列表。对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。程序完成后,按字母顺序对生成的单词进行排序和打印。

这是文件:

但是透过窗外的光线会变得柔和

这是东方,朱丽叶是太阳

升起美丽的太阳,杀死嫉妒的月亮

谁已经病入膏肓了

期望的输出:

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', '羡慕', 'fair', 'grief '、'是'、'杀'、'光'、'月'、'苍白'、'病'、'软'、'太阳'、'之'、'透'、'什么'、'窗'、 '与','那边']

这是我的代码:

fname = raw_input("Enter file name: ")
fh = open(fname)

lst = list()

#loop through the text to get the lines
for line in fh:

    line = line.rstrip()

    #loop through the line to get the words       
    for word in line:
        words = line.split()

        #if a word is not in the empty list, append it       
        if not word in lst: lst.append(word)

lst.sort()
print lst

我的输出:

['', 'A', 'B', 'I', 'J', 'W', 'a', 'b', 'c', 'd', 'e', 'f' ,'g','h','i','k','l','m','n','o','p','r','s','t',' u', 'v', 'w', 'y']

如果您能告诉我出了什么问题(只获取开头有空格的单词的第一个字母而不是整个单词),那就太好了..

注意:我希望代码使用这些指令,而不是其他高级指令(以保持我的学习顺序)

谢谢

【问题讨论】:

  • 当您编写for word in line: 时,Python 将遍历它认为字符串line 的“元素”:也就是说,它将逐个字符地遍历字符串。所以word 总是一个字符。 (请注意,这里有一些混淆:你定义了words,但从不使用它。)

标签: python


【解决方案1】:

您应该拨打电话split(),而不是电话中的文字。

for line in file:
    result = line.split()  # this returns a list of values
    for word in result:
        # check if it already is in your list of words
list.sort()

【讨论】:

    【解决方案2】:

    让我们逐行查看代码

    for line in fh:
        line = line.rstrip()
    

    所以现在我们的第一个line 包含“但是柔和的光线穿过那边的窗户打破”,它是一个字符串。

    for word in line:
    

    啊,但是现在,我们已经说过“让我们遍历line(一个字符串)并让word成为它的每个部分,当我们通过循环时。但是line是一个完整的字符串!当您像这样遍历一个字符串,一次得到一个字母,这就是您在结果中看到的。

    相反,不要有 for 循环,只需像以前一样拆分行:

    for line in fh:
        line = line.rstrip()
        words = line.split()
        for word in words:
            if word not in lst:
                lst.append(word)
    

    【讨论】:

      【解决方案3】:

      试试这个:

      with open('test.txt') as f:
          words = []
          for line in f:
              if line:
                  words.extend(line.split())
      
          print(sorted(set(words)))
      

      输出:

      ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
      

      【讨论】:

        【解决方案4】:

        你缺少的只是这一行

        for word in line.split():    #split() gives you list of word seprated by space 
        

        通过这样做,您正在制作 line => 单词列表

        现在它是 char 列表(一个简单的字符串)。尝试在您的示例中 print word 并在使用 line.split() 后打印 word 你会得到更好的主意。

        查看此链接 > how to use split

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-13
          • 1970-01-01
          • 1970-01-01
          • 2015-12-09
          • 2016-08-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多