Python 文本文件,打印所有以 'the' 开头的单词的列表

Python Text Files, print list with all words that begin with 'the'

我必须编写代码来列出所有以 'the' 开头的单词(例如 there, therefore, then, the)。但列表中没有任何重复项。谁能帮忙?这是我目前所拥有的。

def getbook():
    bookname = input("What is the name of the text file?")
    bookFile = open(bookname, 'r')
    bookString = bookFile.read()
    lowerBook = bookString.lower()
    wordList = lowerBook.split()
    return wordList

import string

def listAllThe(longString):
    theList = []
    for i in longString:
        if i == 'the':
            theList.append( i)
    return theList

def final():
    book = getbook()
    getList = listAllThe(book)
    print (getList)

final()

你应该检查 set 数据类型,它不允许重复,并且在其中搜索是 O(1)(常数时间)。

此外,您应该检查 string.startswith() 函数,如果字符串以作为参数传入的值开头,它将 return 为真。

然后在您的 listAllThe 函数中,您可以使用函数 set()theList 初始化为 set ,然后进行 if 条件检查,如 - i.startswith('the').

修改后的代码看起来像 -

def getbook():
    bookname = input("What is the name of the text file?")
    bookFile = open(bookname, 'r')
    bookString = bookFile.read()
    lowerBook = bookString.lower()
    wordList = lowerBook.split()
    return wordList

import string

def listAllThe(longString):
    theList = set()
    for i in longString:
        if i.startswith('the'):
            theList.add(i)
    return theList

def final():
    book = getbook()
    getList = listAllThe(book)
    print (getList)

final()

这是一种可以通过 Python 列表理解轻松完成的事情。结果列表可用于初始化 set,这将删除重复项:

set([x for x in bookFile.read().lower().split() if x.startswith('the')])