Computer Science, asked by bharathjrsb, 5 months ago

Write a function sentencesByLength that reads the text in the file filename and returns a dictionary where each key represents a number of words, and the corresponding value is a list of those sentences in the file that have that number of words.For example the file sentences1.txt has two sentences with 9 words, so the returned dictionarywill contain an entry with the key 9 and the two sentences of length 9 contained in a list: { 9:['The quick brown fox jumps over the lazy dog.', "There's a message for you if youlook up."], ...}. The quick brown fox jumps over the lazy dog. There's a message for you if you look up. Just go ahead and press that button​

Answers

Answered by jai696
4

\huge\red{\mid{\fbox{\tt{Using\: Python\: 3}}\mid}}

def sentences_by_length(file_name):

words_counter = {}

with open(file_name) as f:

for line in f:

words_len = len(line.split())

if words_len in words_counter:

words_counter.setdefault(words_len, []).append(line)

else:

words_counter[words_len] = [line]

return words_counter

for k, v in sentences_by_length("code.txt").items(): print(f"{k}: {v}")

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Similar questions