Reputation: 45
Alright well I am trying to create a function that updates/ creates two dictionaries to include the data from the open file.
The sample text file would look something like this:
Dunphy, Claire # Name of the person
Parent Teacher Association # The networks person is associated with
Dunphy, Phil # List of friends
Pritchett, Mitchell
Pritchett, Jay
Dunphy, Phil
Real Estate Association
Dunphy, Claire
Dunphy, Luke
Pritchett, Jay
Pritchett, Gloria
Delgado, Manny
Dunphy, Claire
def load_profiles(profiles_file, person_to_friends, person_to_networks):
assume the profiles_file is allready open so no need to pass the argument to open the file
person to friend is a dictionary, each key is a person (str) and each value is that person's friends (list of strs).
person to networks is a dictionary, each key is a person (str) and each value is the networks that person belongs to (list of strs).
So i guess it would be easier dividing this problem in sub function/ helper function, one function that creates the person to friend dictionary and another that creates person to network dictionary.
So far for the person to friend function I have come up with:
def person_to_friend(profiles_file):
person = {}
friends = []
for name in profiles_file:
name = name.strip(). split('\n')
if "," in name and name not in person.keys():
person[key].append(name)
return person
But this is returning a empty dictionary, not sure what I am doing wrong. Also not sure how to add the friends as the values for person.
Upvotes: 1
Views: 1316
Reputation: 2155
You are returning from within the for loop, so basically you return after the first iteration.
Also, I don't quite get name = name.strip().split('\n')
. Isn't name
already a line from profiles_file
?
And also make sure that key
from person[key].append(name)
exists and person[key]
is a list.
I think you should rethink a little your algorithm.
EDIT: Maybe something like this will work for you:
f = open(profiles_file)
person = {}
friends = []
groups = f.read().split('\n\n')
for g in groups:
g = g.split('\n')
person[g[0]] = []
for friend in g[1:]:
if ',' in friend:
person[g[0]].append(friend)
Ofcourse, in order to do it without openning the file more then once you could just create 2 dictionaries or add another key to the existing one, like person[key]['friends']
and person[key]['networks']
. Then put an else
to the last if
.
Upvotes: 0
Reputation: 65791
Your "helper" function tries to iterate over the whole file. While using a separate function to add entries to the dictionary is not such a bad idea, it should probably be done inside a single loop, such as:
def load_profiles(profiles_file, person_to_friends, person_to_networks):
friends = []
networks = []
for line in profiles_file:
if not line.strip(): # we've just finished processing an entry,
# need to get ready for the next one
person_to_friends[name] = friends
person_to_networks[name] = networks
friends = []
networks = []
else:
if friends == networks == []: # we haven't read anything yet,
name = line # so this must be the name
elif ',' in line:
friends.append(line)
else:
associations.append(line)
This is probably over-simplified (for example, it doesn't check that the networks are listed before friends), but it's already too much code for an answer to a homework question. I hope it's compensated with the bugs in it, since I haven't tested it :) Cheers.
Upvotes: 1
Reputation: 23886
Despite the indentation issue in your original problem statement (which would have thrown a syntax error you would have quickly addressed), your person_to_friend
function has a bug in its dictionary invocation: person[key].append(name)
should read person[key] = name
. It otherwise seems fine.
I believe your design can be refined by developing a stronger relational model that connects persons
to friends
, but the entire purpose of your homework exercise is to help teach you how that works! So, I'll instead be coy and not give the farm away on how to redesign your application, addressing only the technical notes here.
I would also inspect Python csv
for parsing your input file contents, because it'll give you a much simpler and more robust model for your underlying data as you try to work through your design.
Otherwise, I just wanted to thank you for providing a wonderful example of how to properly draft a homework
question here on StackOverflow. Formatting aside, this is clear, concise, detail-oriented, and says very good things about your abilities as a programmer as you work through your current classes. Best of luck to you!
Upvotes: 1