无法将文本文件中的最后一个单词追加到列表

时间:2016-07-10 13:12:42

标签: python word-count

我想制作一个文本文件中包含的单词列表。但我的代码打印除文件的最后一个字之外的所有字。我做错了什么?

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)

3 个答案:

答案 0 :(得分:2)

只需使用@app.route('/user-dashboard') @login_required def user_dashboard(): name = current_user.name surname = current_user.surname return render_template('user-dashboard.html', name=name, surname=surname)

即可

我希望这有所帮助,祝你好运。

答案 1 :(得分:1)

当退出循环时,你没有附加最后一个单词,试试这个:

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)

答案 2 :(得分:0)

我认为你错过了决赛:

init?(title: String? = nil, author: String? = nil, price: String? = nil, pubDate: String? = nil) {
    guard let title = title, author = author else { return nil }
    self.title = title
    self.author = author
    self.price = price
    self.pubDate = pubDate
}

// These all return nil
let book5 = Book()
let book6 = Book(title: "War and Peace")
let book7 = Book(author: "Ray Bradbury")
let book8 = Book(title: "Pride and Prejudice", price: "40.00", pubDate: "1813")

因为如果您的文本文件没有以空格结尾,则不会附加