In the above task there are 3 functions: 1. Displays text and performs word counting. 2. Must display each word and the number of its repetitions in the form of a dictionary. 2. Must output each word and the number of its repetitions in the form of named tuples, in the sequence in which the words are in the text.

import argparse import sys def main(): parser = argparse.ArgumentParser() parser.add_argument("infile", help="Text file to be analyzed.") args = parser.parse_args() with open(args.infile, encoding="utf-8") as f: text = f.read() words = text.split() unique_words(words) def unique_words(words): the_dict = {} for line in words: unique_words(the_dict) return(the_dict) def count_unique_sorted(words): words = [] return(words) if __name__ == "__main__": sys.exit(main()) 

Directly text (.txt):

 Design, develop, maintain and test cloud applications in Python, and document API for cloud services. design, develop, and test cloud applications in Python, and document API for services. Design, , and cloud in Python, and document for services. 
  • what have u see the difficulty Give a specific example of input / output. What did you expect to receive? What happens instead? The minimum reproducible example is jfs

1 answer 1

Since you have not indicated the desired result, I am not sure that my solution is right for you, here is my option:

 from collections import Counter from collections import namedtuple import sys def data(): with open(sys.argv[1], 'r') as file: return file.read().strip() def first(arg): # Подсчет слов return f'Результат первой функции {len(arg.split())}\n' def two(arg): # Слово и его кол-во return f'Результат второй функции {Counter(arg.split())}\n' def three(arg): # Слово и его кол-во в виде namedtuple name = namedtuple('NAME', ['word', 'count']) return f'Результат третьей функции {tuple(name(word, arg.count(word)) for word in set(arg.split()))}' 

Usage: when starting the script, pass 1 parameter to cmd the path to the file, and then:

 print(first(data())) print(two(data())) print(three(data())) 

The result for the data you specified in the question:

 Результат первой функции 37 Результат второй функции Counter({'and': 6, 'cloud': 4, 'in': 3, 'Python,': 3, 'document': 3, 'for': 3, 'Design,': 2, 'develop,': 2, 'test': 2, 'applications': 2, 'API': 2, 'services.': 2, 'maintain': 1, 'design,': 1, 'services': 1}) Результат третьей функции (NAME(word='API', count=2), NAME(word='cloud', count=4), NAME(word='document', count=3), NAME(word='Python,', count=3), NAME(word='for', count=3), NAME(word='services.', count=2), NAME(word='test', count=2), NAME(word='in', count=5), NAME(word='Design,', count=2), NAME(word='develop,', count=2), NAME(word='applications', count=2), NAME(word='services', count=3), NAME(word='and', count=6), NAME(word='design,', count=1), NAME(word='maintain', count=1))