试图创建一个基于 bool 的函数
trying to create a function based on a bool
我现在有这个东西,但第一部分有点问题,因为答案应该是 12,因为我的文件有 12 章,但我得到 2726 作为答案。
我还需要制作第四个函数:
analyze_book(filename, chapter_delimiter, word)
其中的任务是使用其他三个函数来输入一个单词,计算该单词在本书每一章中出现的次数,然后将结果绘制在 x-axes 的图表中是章节编号,y-axes 是该词在该章节中出现的次数。
格拉夫也将看起来像这样:
- graf 的标题是:文件名中每章显示的字数
- 标题x-axis:章节
- 标题y-axis:字数
- x-axis 从 1 到章节数
- y-axis 从 0 到最高数量 og 乘以单词 +3
正如顶部所说,def get_chapter 在某个地方有点不确定,我不知道为什么
而且我也缺少最后一个功能,我不知道从这里去哪里。
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = 0
for line in lines:
chapters = line.split(chapter_delimiter)
chapters = [chapter.strip() for chapter in chapters if len(chapter.strip())!=0]
num_chapters += len(chapters)
return num_chapters
print(get_chapters('alice_in_wonderland.txt', 'CHAPTER'))
def count_words(string_list, word):
counts = []
for string in string_list:
count = 0
for i in range(len(string)):
if string[i:i + len(word)].lower() == word.lower():
count += 1
counts.append(count)
return counts
strengliste = ["Takpapp, veggpapp, papp og papir", "Papir, stein, saks og papir"]
count_words(strengliste, "Papir")
def create_number_to(number):
lst = []
for i in range(1, number + 1):
lst.append(i)
return lst
print(create_number_to(14))
如果能得到任何帮助,我将不胜感激
这是我对这个问题的了解这是问题代码的作者
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = 0
for line in lines:
chapters = line.split(chapter_delimiter)
chapters = [chapter.strip() for chapter in chapters if len(chapter.strip())!=0]
num_chapters += len(chapters)
return num_chapters
print(get_chapters('alice_in_wonderland.txt', 'CHAPTER'))
def count_words(string_list, word):
counts = []
for string in string_list:
count = 0
for i in range(len(string)):
if string[i:i + len(word)].lower() == word.lower():
count += 1
counts.append(count)
return counts
strengliste = ["Takpapp, veggpapp, papp og papir", "Papir, stein, saks og papir"]
count_words(strengliste, "Papir")
def create_number_to(number):
lst = []
for i in range(1, number + 1):
lst.append(i)
return lst
print(create_number_to(14))
你的章节计数功能可以这样实现:
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = len(lines.split(chapter_delimiter))
return num_chapters
我现在有这个东西,但第一部分有点问题,因为答案应该是 12,因为我的文件有 12 章,但我得到 2726 作为答案。 我还需要制作第四个函数:
analyze_book(filename, chapter_delimiter, word)
其中的任务是使用其他三个函数来输入一个单词,计算该单词在本书每一章中出现的次数,然后将结果绘制在 x-axes 的图表中是章节编号,y-axes 是该词在该章节中出现的次数。 格拉夫也将看起来像这样:
- graf 的标题是:文件名中每章显示的字数
- 标题x-axis:章节
- 标题y-axis:字数
- x-axis 从 1 到章节数
- y-axis 从 0 到最高数量 og 乘以单词 +3
正如顶部所说,def get_chapter 在某个地方有点不确定,我不知道为什么 而且我也缺少最后一个功能,我不知道从这里去哪里。
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = 0
for line in lines:
chapters = line.split(chapter_delimiter)
chapters = [chapter.strip() for chapter in chapters if len(chapter.strip())!=0]
num_chapters += len(chapters)
return num_chapters
print(get_chapters('alice_in_wonderland.txt', 'CHAPTER'))
def count_words(string_list, word):
counts = []
for string in string_list:
count = 0
for i in range(len(string)):
if string[i:i + len(word)].lower() == word.lower():
count += 1
counts.append(count)
return counts
strengliste = ["Takpapp, veggpapp, papp og papir", "Papir, stein, saks og papir"]
count_words(strengliste, "Papir")
def create_number_to(number):
lst = []
for i in range(1, number + 1):
lst.append(i)
return lst
print(create_number_to(14))
如果能得到任何帮助,我将不胜感激
这是我对这个问题的了解这是问题代码的作者
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = 0
for line in lines:
chapters = line.split(chapter_delimiter)
chapters = [chapter.strip() for chapter in chapters if len(chapter.strip())!=0]
num_chapters += len(chapters)
return num_chapters
print(get_chapters('alice_in_wonderland.txt', 'CHAPTER'))
def count_words(string_list, word):
counts = []
for string in string_list:
count = 0
for i in range(len(string)):
if string[i:i + len(word)].lower() == word.lower():
count += 1
counts.append(count)
return counts
strengliste = ["Takpapp, veggpapp, papp og papir", "Papir, stein, saks og papir"]
count_words(strengliste, "Papir")
def create_number_to(number):
lst = []
for i in range(1, number + 1):
lst.append(i)
return lst
print(create_number_to(14))
你的章节计数功能可以这样实现:
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = len(lines.split(chapter_delimiter))
return num_chapters