无法将文本文件中的最后一个单词附加到列表中
Can't append the last word from a text file to a list
我想制作一个包含在文本文件中的单词列表。但是我的代码打印了文件的最后一个字以外的所有内容。我做错了什么?
def word_map(file):
text = file.read()
word = "" # used as a temporary variable
wordmap = []
for letter in text:
if letter != " ":
word = word+letter
else:
wordmap.append(word)
word = ""
return set(wordmap)
退出循环时,您没有附加最后一个词,只需尝试以下操作:
def word_map(file):
text = file.read()
word = "" # used as a temporary variable
wordmap = []
for letter in text:
if letter != " ":
word = word+letter
else:
wordmap.append(word)
word = ""
wordmap.append(word)
return set(wordmap)
我想你错过了决赛:
wordmap.append(word)
因为如果您的文本文件不以 space 结尾,则不会附加
只需使用wordmap = text.split(" ")
希望对你有所帮助,祝你好运。
我想制作一个包含在文本文件中的单词列表。但是我的代码打印了文件的最后一个字以外的所有内容。我做错了什么?
def word_map(file):
text = file.read()
word = "" # used as a temporary variable
wordmap = []
for letter in text:
if letter != " ":
word = word+letter
else:
wordmap.append(word)
word = ""
return set(wordmap)
退出循环时,您没有附加最后一个词,只需尝试以下操作:
def word_map(file):
text = file.read()
word = "" # used as a temporary variable
wordmap = []
for letter in text:
if letter != " ":
word = word+letter
else:
wordmap.append(word)
word = ""
wordmap.append(word)
return set(wordmap)
我想你错过了决赛:
wordmap.append(word)
因为如果您的文本文件不以 space 结尾,则不会附加
只需使用wordmap = text.split(" ")
希望对你有所帮助,祝你好运。