5 simple programming problems involving Python dictionaries




1. **Dictionary Initialization:**
   Write a program that initializes an empty dictionary called `my_dict` and adds three key-value pairs: "name" with a value of your name, "age" with a value of your age, and "city" with a value of your city.

2. **Word Frequency Counter:**
   Create a function that takes a string as input and returns a dictionary containing the frequency of each word in the string. Ignore case and punctuation. For example, for the input "Hello world. Hello Python.", the output should be `{'hello': 2, 'world': 1, 'python': 1}`.

3. **Dictionary Merging:**
   Write a program that defines two dictionaries, `dict1` and `dict2`. Merge these dictionaries into a new one called `merged_dict` and print the result. If there are common keys, sum their values. For example:
   ```python
   dict1 = {'a': 10, 'b': 20}
   dict2 = {'b': 30, 'c': 40}
   merged_dict = {...}  # Your code here
   # Output should be {'a': 10, 'b': 50, 'c': 40}
   ```

4. **Removing Duplicates:**
   Create a function that takes a list of numbers as input and returns a dictionary with the unique numbers as keys and their frequencies as values. For example, for the input `[1, 2, 2, 3, 4, 4, 4]`, the output should be `{1: 1, 2: 2, 3: 1, 4: 3}`.

5. **Nested Dictionary Access:**
   Define a nested dictionary representing a person's contact information with keys such as "name," "address," and "phone." Write a function that takes the person's name and a key as input and returns the corresponding value. Make sure to handle cases where the name or key is not present in the dictionary.

Contact us for software training, education or development










 

Post a Comment

0 Comments