234k views
1 vote
Write a program that reads a list of words. Then, the program outputs those words and their frequencies (case insensitive).

a) Implement the program using Python.
b) Utilize a dictionary to store word frequencies.
c) Ensure case insensitivity using the lower() function.
d) Display the output in αbetical order.

User Hartshoj
by
7.4k points

1 Answer

1 vote

Final answer:

This detailed answer explains how to implement a Python program that counts word frequencies in a list of words while ensuring case insensitivity and displaying the output in alphabetical order.

Step-by-step explanation:

Python Program to Count Word Frequencies

To implement the program, follow the steps below:

  1. Create an empty dictionary to store the word frequencies.
  2. Read the list of words from the user.
  3. Iterate over each word in the list and convert it to lowercase using the lower() function.
  4. If the word is already in the dictionary, increment its frequency by 1. Otherwise, add it to the dictionary with a frequency of 1.
  5. Sort the dictionary by keys using the sorted() function.
  6. Display each word and its frequency from the sorted dictionary.

Example:

word_list = ['Apple', 'banana', 'Orange', 'apple', 'banana']

word_frequencies = {}

for word in word_list:
word = word.lower()
if word in word_frequencies:
word_frequencies[word] += 1
else:
word_frequencies[word] = 1

sorted_word_frequencies = sorted(word_frequencies.items())

for word, frequency in sorted_word_frequencies:
print(word, frequency)

User Galivan
by
7.6k points