好的,所以我试着解决这个关于在文本文件中找到单词的练习,它应该打印出多少单词。所以我试着先把单词分开,这样就更容易找到特定的单词了。然而,当我试图把两段分开时,它只分裂了第二段。
文本文件是这样写的:
“比尔和泰德的卓越冒险是一部1989年美国科幻喜剧巴迪电影,也是比尔·特德系列电影中的第一部电影,两名懒汉穿越时间聚集了一大群历史人物,为他们的高中历史演讲。”
这部电影由克里斯·马西森和埃德·所罗门执导,斯蒂芬·赫雷克执导。基努·里维斯饰演泰德·“西奥多”·洛根,亚历克斯·温特饰演比尔·S·普雷斯顿,埃斯奎尔和乔治·卡林饰演鲁弗斯。比尔和泰德的优秀冒险获得了好评,大多是积极的发布和商业上的成功。它现在被认为是一个邪教经典。两年后上映了一部续集“比尔和泰德的假旅程”。一部未命名的第三部电影正在制作中。“
这样做的目的是找出段落中有多少个“比尔”。
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“比尔”,因为它只读了第二段。我试着寻找和我有同样问题的人,但似乎只有我一个人。请注意,程序可以打印前两段而不分裂。
发布于 2014-03-23 06:43:57
你的压痕是错误的,应该是:
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)发布于 2014-03-23 07:44:16
在字符串中查找单词有一种更容易的方法。只需使用字符串的计数法
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"))https://stackoverflow.com/questions/22587859
复制相似问题