我希望在一篇文章中统计一下“血”这个词的具体提及次数,但我还没有走得太远。我已经尝试了几种方法,最大的收获是能够解析出包含该单词的所有句子。这就是我到目前为止得到的结果,它以退出代码0结束
infile = open('C:\IS452\week7\dracula.txt', 'rt', encoding = 'utf=8')
dracula_lines = infile.readlines()
infile.close()
for blood_lines in dracula_lines:
accumulator = blood_lines.strip()
dracula_lines.count("blood")
if "blood" in dracula_lines:
print("blood") in str(newText[blood_lines + 1])
#print(blood_lines)
blood_lines = accumulator.split("blood")
newText = ("There are this many mentions of blood in Dracula", blood_lines)
print(newText)发布于 2019-10-13 01:12:31
你可以直接在字符串上使用.count():
with open('C:\IS452\week7\dracula.txt', 'r', encoding = 'utf=8') as f:
dracula_txt = f.read().lower()
blood_count = dracula_txt.count("blood")
print(blood_count, "mentions of blood in Dracula")如果您这样做是为了一些编码挑战,而不允许只使用.count(),那么您可以这样做:
dracula_list = dracula_txt.split("blood") # split text into list separated by occurrences of "blood"
blood_count = len(dracula_list) - 1 # count of occurrences is the length of the list minus one如果你认为这也是作弊,那么你可以试着这样做:
index = blood_count = 0
while(index != -1):
try:
# we get the position of the next occurrence of "blood" in the string, starting from the position of the last occurrence plus one
index = dracula_txt.index("blood", index) + 1
blood_count += 1
except ValueError: # when there aren't any more occurrences of "blood" in the string, we get a ValueError and exit the loop
index = -1
print(blood_count, "mentions of blood in Dracula")或者您可以遍历字符串中的每个字符,检查字符索引后面的子字符串是否与“and”匹配,并相应地递增计数:
blood_count = 0
for i in range(0, len(dracula_txt)):
if dracula_txt[i:i+5] == "blood":
blood_count += 1
print(blood_count)https://stackoverflow.com/questions/58356123
复制相似问题