Lab 10: Dictionaries
Program 1: Count Vowels
countvowels.py
'''
Author name: <Your name here>
This program will count vowels in a string of vowels.
'''
def countVowels(vowel_string):
# Create an empty dictionary
dictionary = {}
# Convert the string to lower case
vowel_string = vowel_string.lower()
# For each character in the vowel string
for char in vowel_string:
# If that character is in the dictionary
if char in dictionary:
# Increment the value by one
dictionary[char] = dictionary[char] + 1
else:
# Otherwise, set the value to one
dictionary[char] = 1
# Return the dictionary
return dictionary
# Input the string of vowels from the user
string_of_vowels = input("Enter a string of vowels: ")
# Call the count vowels function
count_of_vowels = countVowels(string_of_vowels)
# Print out the resulting dictionary
print(count_of_vowels)
Program 2: Unique Values
uniquevalues.py
'''
Author name: <Your name here>
This program will extracts unique values from a dictionary of lists
and print a new list of unique values.
'''
# The dictionary whose values are lists of grades
dictionary_of_lists = {'Student 1': [100, 98, 97, 96], 'Student 2': [88, 97, 75, 100], 'Student 3': [98,
97, 96, 77]}
# Create a new list to store the unique values.
unique_values_list = []
# For each grade list in the dictionary.
for grade_list in dictionary_of_lists.values():
# For each grade in the grade list.
for grade in grade_list:
# If the grade is not in the unique values list, then add it.
if grade not in unique_values_list:
# Append the grade to the list.
unique_values_list.append(grade)
# Print the resulting unique values list.
print(unique_values_list)
Program 3: Min Max Mean
minMaxMean.py
# Create a list with some test number values.
lst = [10, 12, 32, 4]
# Create an empty dictionary.
d = {}
# Add the mean value to the dictionary using the key "Mean Value"
d["Mean Value"] = sum(lst) / len(lst)
# Add the max value to the dictionary using the key "Maximum Value"
d["Maximum Value"] = max(lst)
# Add the min value to the dictionary using the key "Minimum Value"
d["Minimum Value"] = min(lst)
# Test the code by printing the dictionary d.
print(d)