好的,我正在尝试解决这个关于在文本文件中查找单词的练习,它应该打印单词的数量。所以我要做的是先把单词分开,这样更容易找到特定的单词。然而,当我试图分割两个段落时,它只会分割第二个段落。
这是文本文件的内容:
"法案,《泰德的伟大冒险》是一部1989年上映的美国科幻喜剧电影,也是比尔·盖茨系列电影的第一部。
本片由Chris Matheson和Ed Solomon编剧,Stephen Herek导演。基努·里维斯饰演泰德·"西奥多"·洛根,亚历克斯·温特饰演比尔·普雷斯顿,《时尚先生》和乔治·卡林饰演鲁弗斯。比尔,《Ted’s Excellent Adventure》一经发行便获得了正面评价,并获得了巨大的商业成功。它现在被认为是一部邪教经典。续集《比尔》《泰德的虚假之旅》两年后出版。尚未命名的第三部电影正在制作中
这个想法是寻找段落中有多少个"Bill"。
f = open("text.txt", "r")
listBill = []
for line in f:
print(line)
splitParagraph = line.split()
print(splitParagraph)
for eachBill in splitParagraph:
if eachBill == "Bill":
listBill.append("Bill")
print(listBill)
但是我得到的结果是:
Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film
and the first film in the Bill & Ted franchise in which two slackers travel through time
to assemble a menagerie of historical figures for their high school history resentation.
The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It
stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and
George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were
mostly positive upon release and was commercially successful. It is now considered a
cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An
untitled third film is in development
['The', 'film', 'was', 'written', 'by', 'Chris', 'Matheson', 'and', 'Ed', 'Solomon',
'and', 'directed', 'by', 'Stephen', 'Herek.', 'It', 'stars', 'Keanu', 'Reeves', 'as',
'Ted', '"Theodore"', 'Logan,', 'Alex', 'Winter', 'as', 'Bill', 'S.', 'Preston,',
'Esquire,', 'and', 'George', 'Carlin', 'as', 'Rufus.', 'Bill', '&', "Ted's",
'Excellent', 'Adventure', 'received', 'reviews', 'which', 'were', 'mostly',
positive', 'upon', 'release', 'and', 'was', 'commercially', 'successful.', 'It', 'is',
now', 'considered', 'a', 'cult', 'classic.', 'A', 'sequel,', 'Bill', '&', "Ted's",
Bogus', 'Journey,', 'was', 'released', 'two', 'years', 'later.', 'An', 'untitled',
'third', 'film', 'is', 'in', 'development']
['Bill', 'Bill', 'Bill']
如你所见,它只找到了3个"Bill",因为它只读了第二段。我试着寻找和我有同样问题的人,但似乎只有我一个人。请注意,程序可以不拆分地打印前两段。
你的缩进不对,应该是:
f = open("text.txt", "r")
listBill = []
for line in f:
print(line)
splitParagraph = line.split()
print(splitParagraph)
for eachBill in splitParagraph:
if eachBill == "Bill":
listBill.append("Bill")
print(listBill)
有一种更简单的方法来查找字符串中的单词。只需使用字符串的count方法:
s = """Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film and the first film in the Bill & Ted franchise in which two slackers travel through time to assemble a menagerie of historical figures for their high school history presentation.
The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were mostly positive upon release and was commercially successful. It is now considered a cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An untitled third film is in development"""
print(s.count("Bill"))