Write a code to print string and int in a map that repeats in a paragraph?
Anonymous
from collections import defaultdict # Define the paragraph paragraph = """ The quick brown fox jumps over the lazy dog. The quick brown fox jumps again. 123 is a number, and 123 is repeated. """ # Initialize a dictionary to count occurrences of words and numbers count_map = defaultdict(int) # Convert the paragraph to lowercase for case-insensitive comparison paragraph = paragraph.lower() # Define punctuation characters to be removed punctuations = ".,!?;:" # Remove punctuation and split the paragraph into words cleaned_words = [] for char in paragraph: if char in punctuations: paragraph = paragraph.replace(char, ' ') # Split the cleaned paragraph into words words = paragraph.split() # Count occurrences of each word or number for word in words: if word.isalnum(): # Check if the word is alphanumeric count_map[word] += 1 # Print the words and numbers that repeat (appear more than once) print("Words and numbers that repeat in the paragraph:") for key, count in count_map.items(): if count > 1: print(f"'{key}': {count}")
Check out your Company Bowl for anonymous work chats.