擅长:python、mysql、java
<p>您应该签出<a href="https://docs.python.org/2/library/stdtypes.html#types-set" rel="nofollow">^{<cd1>}</a>数据类型,它不允许重复,并且在其中搜索是O(1)(恒定时间)。在</p>
<p>另外,您应该签出<a href="https://docs.python.org/2/library/stdtypes.html#str.startswith" rel="nofollow">^{<cd2>}</a>函数,如果字符串以作为参数传入的值开始,它将返回true。在</p>
<p>然后在<code>listAllThe</code>函数中,可以使用函数<code>set()</code>将{<cd4>}初始化为<code>set</code>,然后进行if条件检查,如-<code>i.startswith('the')</code>。在</p>
<p>更改后的代码看起来像-</p>
<pre><code>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()
</code></pre>