Python Forum
Problema función listas y diccionarios
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problema función listas y diccionarios
#1
Good afternoon, I'm stuck because I do not know how to tackle the next task, someone could give me a hand on how to approach it
thank you

issue

If we have a dictionary that contains as keys the name of a person and as a value a list with their "personal preferences" that can be, for example "swim", "read", "go to the movies", etc. You want to program a function AddPreference (dictionary, person, preference) such that:
o If the person does not exist, add it to the dictionary with a list that contains only one element.
o If the person exists and the current preference exists on your list, it has no effect, but if that preference does not exist on your list, add it to the list.
Once the above is completed, make a program that adds information to the dictionary and once it has that information included, ask the user for a preference by keyboard and list on the screen the name of all the people who have that preference.
Reply
#2
You can use the in operator to tell if a person is in the dictionary. If they are, you can use append to add the item to the list (again using the in operator to see that it's not already there).

For the second part I would use the items method of the dictionary to loop through the key/value pairs, accumulating or printing the keys where the preference is in the value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020