I'm in need of some assistance in this code problem from a MOOC on python programming that I'm taking. This is just for self-learning, and not for any graded coursework. Could you please provide some guidance. I am stuck. Thanks in advance for your help.
The problem statement is: One of the confusing things about dictionaries is that they are unordered: the keys have no internal ordering to them. Sometimes though, you want to look through the keys in a particular order, such as printing them alphabetically if they represent something like artist names.
For example, imagine if a forum tool used for a course exported its data as a dictionary. The keys of the dictionary are students' names, and the values are days of activity. Your goal is to return a list of students in the class in alphabetical order, followed by their days of activity, like this: Chopra, Deepak: 22 Joyner, David: 14 Winfrey, Oprah: 17
Write a function named alphabetical_keys. alphabetical_keys should take as input a dictionary, and return a single string. The keys of the dictionary will be names and the values will be integers. The output should be a single string made of multiple lines, following the format above: the name (the key), a colon and space, then the number of days of activity (the value), sorted alphabetically by key.
Remember, you are returning this as a single string: you're going to need to put the \n character after each line.
To convert a dictionary's keys into a list, use this line of code: keys_as_list = list(the_dict.keys)
From there, you could sort keys_as_list like any normal list.
Add your code here!
def alphabetical_keys(dictionary):
keys_as_list = list(dictionary.keys())
return keys_as_list dictionary[keys]
Below are some lines of code that will test your function. You can change the value of the variable(s) to test your function with different inputs.
If your function works correctly, this will originally print: Chopra, Deepak: 22 Joyner, David: 14 Winfrey, Oprah: 17
the_dictionary = {"Joyner, David": 14, "Chopra, Deepak": 22, "Winfrey, Oprah": 17}
print(alphabetical_keys(the_dictionary))
CodePudding user response:
While dictionaries may seem like they should be ordered, it's best not to think about them that way. It's a mapping from one thing to another.
You already have a way to get a list of the names in the dict:
keys_as_list = list(dictionary.keys())
After a quick Google search on "how to sort python list":
# sort list in place
keys_as_list.sort() # reverse=True would give reverse alpha order
Now you just need to loop through the sorted name as a way to access the dictionary keys:
return_str = "" # start with an empty string
for name in keys_as_list:
# add to the string -- get the name and the dict value for that name
return_str = f"{name}: {dictionary[name]}\n"
All together:
def alphabetical_keys(dictionary):
keys_as_list = list(dictionary.keys())
keys_as_list.sort()
return_str = ""
for name in keys_as_list:
return_str = f"{name}: {dictionary[name]}\n"
return return_str
d = {
"Chopra, Deepak": 22,
"Joyner, David": 14,
"Winfrey, Oprah": 17,
"Gump, Forrest": 9,
"Obama, Barack": 19,
}
string = alphabetical_keys(d)
print(string)
Output:
Chopra, Deepak: 22
Gump, Forrest: 9
Joyner, David: 14
Obama, Barack: 19
Winfrey, Oprah: 17