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.
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!
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.
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.
Related
I am looking for a solution to a problem, I am trying to make a list on Python that has a way of making a list with People and a set skill like (John= ForkLift Trained, CDL, Jackhammer, electrician.) Something like that but with like 20 names and every trade that an Organizational Leader Degree can use? Can someone help me?
You did not provide any code here to show us what you tried to solve the problem, also you did not specify how knowledgeable you are in python, so it is a bit hard to understand your background approaching this problem.
Here is a possible solution for your issue: create a dictionary with the names of the different employees, and assign to each key in the dictionary an array with the skills of that employee.
For example:
employees = {}
employees['John'] = ["ForkLift Trained", "CDL", "Jackhammer", "electrician"]
employees['Brian'] = ["Computer Scientist", "Programmer"]
print(employees)
for k in employees:
print('worker: {} has the following skills: {}'.format(k, employees[k]))
The formatting of the print function can obviously be changed as needed, but this is the general idea.
The output for this code will be:
{'John': ['ForkLift Trained', 'CDL', 'Jackhammer', 'electrician'], 'Brian': ['Computer Scientist', 'Programmer']}
worker: John has the following skills: ['ForkLift Trained', 'CDL', 'Jackhammer', 'electrician']
worker: Brian has the following skills: ['Computer Scientist', 'Programmer']
Again this is only a rough idea, the problem you described is quite general and can be solved in many different ways unless you provide more information.
Edit: the print function for the separate name and skills of each employee is:
for k in employees:
print('worker: {} has the following skills: {}'.format(k, employees[k]))
This function will work in printing a single worker or a 1000 workers if enough were loaded inside the dictionary (which was declared using employees ={}).
Every employee will have his own seperate line of output.
In order to print the name of an employee with his different skills each in a different line it is possible to use something like this:
for k in employees:
print('{} has the following skills:'.format(k))
for i in employees[k]:
print(i)
print()
and the output will be:
John has the following skills:
ForkLift Trained
CDL
Jackhammer
electrician
Brian has the following skills:
Computer Scientist
Programmer
I'm building a de-identify tool. It replaces all names by other names.
We got a report that <name>Peter</name> met <name>Jane</name> yesterday. <name>Peter</name> is suspicious.
outpout :
We got a report that <name>Billy</name> met <name>Elsa</name> yesterday. <name>Billy</name> is suspicious.
It can be done on multiple documents, and one name is always replaced by the same counterpart, so you can still understand who the text is talking about. BUT, all documents have an ID, referring to the person this file is about (I'm working with files in a public service) and only documents with the same people ID will be de-identified the same way, with the same names. (the goal is to watch evolution and people's history) This is a security measure, such as when I hand over the tool to a third party, I don't hand over the key to my own documents with it.
So the same input, with a different ID, produces :
We got a report that <name>Henry</name> met <name>Alicia</name> yesterday. <name>Henry</name> is suspicious.
Right now, I'm hashing each name with the document ID as a salt, I convert the hash to an integer, then subtract the length of the name list until I can request a name with that integer as an indice. But I feel like there should be a quicker/more straightforward approach ?
It's really more of an algorithmic question, but if it's of any relevance I'm working with python 2.7 Please request more explanation if needed. Thank you !
I hope it's clearer this way ô_o Sorry when you are neck-deep in your code you forget others need a bigger picture to understand how you got there.
As #LutzHorn pointed out, you could just use a dict to map real names to false ones.
You could also just do something like:
existing_names = []
for nameocurrence in original_text:
if not nameoccurence.name in existing_names:
nameoccurence.id = len(existing_names)
existing_names.append(nameoccurence.name)
else:
nameoccurence.id = existing_names.index(nameoccurence.name)
for idx, _ in enumerate(existing_names):
existing_names[idx] = gimme_random_name()
Try using a dictionary of names.
import re
names = {"Peter": "Billy", "Jane": "Elsa"}
for name in re.findall("<name>([a-zA-Z]+)</name>", s):
s = re.sub("<name>" + name + "</name>", "<name>"+ names[name] + "</name>", s)
print(s)
Output:
'We got a report that <name>Billy</name> met <name>Elsa</name> yesterday. <name>Billy</name> is suspicious.'
i have list like this
["<name:john student male age=23 subject=\computer\sience_{20092973}>",
"<name:Ahn professor female age=61 subject=\computer\math_{20092931}>"]
i want to get student using {20092973},{20092931}.
so i want to split to list like this
my expect result 1 is this (input is {20092973})
"student"
my expect result 2 is this (input is {20092931})
"professor"
i already searching... but i can't find.. sorry..
how can i this?
I don't think you should be doing this in the first place. Unlike your toy example, your real problem doesn't involve a string in some clunky format; it involves a Scapy NetworkInterface object. Which has attributes that you can just access directly. You only have to parse it because for some reason you stored its string representation. Just don't do that; store the attributes you actually want when you have them as attributes.
The NetworkInterface object isn't described in the documentation (because it's an implementation detail of the Windows-specific code), but you can interactively inspect it like any other class in Python (e.g., dir(ni) will show you all the attributes), or just look at the source. The values you want are name and win_name. So, instead of print ni, just do something like print '%s,%s' % (ni.name, ni.win_name). Then, parsing the results in some other program will be trivial, instead of a pain in the neck.
Or, better, if you're actually using this in Scapy itself, just make the dict directly out of {ni.win_name: ni.name for ni in nis}. (Or, if you're running Scapy against Python 2.5 or something, dict((ni.win_name, ni.name) for ni in nis).)
But to answer the question as you asked it (maybe you already captured all the data and it's too late to capture new data, so now we're stuck working around your earlier mistake…), there are three steps to this: (1) Figure out how to parse one of these strings into its component parts. (2) Do that in a loop to build a dict mapping the numbers to the names. (3) Just use the dict for your lookups.
For parsing, I'd use a regular expression. For example:
<name:\S+\s(\S+).*?\{(\d+)\}>
Debuggex Demo
Now, let's build the dict:
r = re.compile(r'<name:\S+\s(\S+).*?\{(\d+)\}>')
matches = (r.match(thing) for thing in things)
d = {match.group(2): match.group(1) for match in matches}
And now:
>>> d['20092973']
'student'
Code:
def grepRole(role, lines):
return [line.split()[1] for line in lines if role in line][0]
l = ["<name:john student male age=23 subject=\computer\sience_{20092973}>",
"<name:Ahn professor female age=61 subject=\compute\math_{20092931}>"]
print(grepRole("{20092973}", l))
print(grepRole("{20092931}", l))
Output:
student
professor
current_list = ["<name:john student male age=23 subject=\computer\sience_{20092973}>", "<name:Ahn professor female age=61 subject=\computer\math_{20092931}>"]
def get_identity(code):
print([row.split(' ')[1] for row in current_list if code in row][0])
get_identity("{20092973}")
regular expression is good ,but for me, a rookie, regular expression is another big problem...
ok so.. i'm trying to write a program that creates a dictionary of son:father entries and another dictionary that contains father:son entries. The program must present the user a menu with five options.
text file is this: john:fred, fred:bill, sam:tony, jim:william, william:mark, krager:holdyn, danny:brett, danny:issak, danny:jack, blasaen:zade, david:dieter, adamLseth, seth:enos
Problem Statement:
Write a program that creates a dictionary of son:father entries and another dictionary that contains father:son entries. Your program must present the user a menu with five options. The following is an example only:
Father/Son Finder
0 – Quit
1 – Find a Father
2 – Find a Grandfather
3 – Find a Son
4 – Find a Grandson
Option 0 ends the program.
Option 1 prompts the user for the name of a son. If the dictionary contains the son:father pair, the program displays the father. Otherwise, the program should tell the user it does not know who the father is.
Option 2 prompts the user for the name of a grandson. If the dictionary contains enough information, the program displays the grandfather. Otherwise, the program should tell the user it does not know who the grandfather is.
Option 3 prompts the user for the name of a father. If the dictionary contains the son:father pair, the program displays the son. Otherwise, the program should tell the user it does not know who the son is.
Option 4 prompts the user for the name of a grandfather. If the dictionary contains enough information, the program displays the grandson. Otherwise, the program should tell the user it does not know who the grandson is.
The program must create the dictionary structure and populate it from data contained in a file provided to you. In addition, the program must continue to ask the user for a menu choice until the user chooses to quit.
I have this thus far. I haven't gotten very far in it...
sons_fathers = {}
fathers_sons = {}
#open filename: names.dat
fo = open("names.dat", "r")
data = fo.read()
print (data)
for line in fo:
here is the flow chart: ![Flow chart][1]
https://jsu.blackboard.com/bbcswebdav/pid-2384378-dt-content-rid-3427920_1/xid-3427920_1
Thanks for the help. I need it lol.
Let's hope nobody give you an exact solution to this homework.
Here some hints, you need to know what you can do with string, string.split() will help you a lot. Also, read about what you can do with dictionary. You will also need the raw_input function
The rest is simple programming. Good luck.
How you describe your solution, I don't think a dictionary is what you want for this.
The keys must be unique.
# wont work, keys aren't unique
father_son = {'danny':'brett', 'danny':'issak', 'danny':'jack'}
You could however try a dictionary with a list as the value:
father_son = {'danny':['brett','issak', 'jack']}
if 'danny' in father_son.keys() and 'brett' in father_son['danny']:
#do something
Or you could use a list of 2-tuples that stores the pairs:
father_son = [('danny', 'brett'), ('danny', 'issak'), ('danny', 'jack')]
if ('danny', 'brett') in father_son:
#do something
sons_fathers = {} # one father per son
fathers_sons = {} # one or many sons per father, use list or
# set for the values
with open("names.dat", "r") as fo: # use context manager to close file automatically
for line in fo: # ?? is there only one line, or one pair per line??
# do something with line
# assume you extracted "son" and "father"
sons_fathers[son] = father
if father in fathers_sons:
fathers_sons[father].add(son)
else:
fathers_sons[father] = {son}
This is my first post, so I'm sorry if I do anything wrong. That said, I searched for the question and found something similar that was never answered due to the OP not giving sufficient information. This is also homework, so I'm just looking for a hint. I really want to get this on my own.
I need to read in a debate file (.txt), and pull and store all of the lines that one candidate says to put in a word cloud. The file format is supposed to help, but I'm blanking on how to do this. The hint is that each time a new person speaks, their name followed by a colon is the first word in the first line. However, candidates' data can span multiple lines. I am supposed to store each person's lines separately. Here is a sample of the file:
LEHRER: This debate and the next three -- two presidential, one vice
presidential -- are sponsored by the Commission on Presidential
Debates. Tonight's 90 minutes will be about domestic issues and will
follow a format designed by the commission. There will be six roughly
15-minute segments with two-minute answers for the first question,
then open discussion for the remainder of each segment.
Gentlemen, welcome to you both. Let's start the economy, segment one,
and let's begin with jobs. What are the major differences between the
two of you about how you would go about creating new jobs?
LEHRER: You have two minutes. Each of you have two minutes to start. A
coin toss has determined, Mr. President, you go first.
OBAMA: Well, thank you very much, Jim, for this opportunity. I want to
thank Governor Romney and the University of Denver for your
hospitality.
There are a lot of points I want to make tonight, but the most
important one is that 20 years ago I became the luckiest man on Earth
because Michelle Obama agreed to marry me.
This is what I have for a function so far:
def getCandidate(myFile):
file = open(myFile, "r")
obama = []
romney = []
lehrer = []
file = file.readlines()
I'm just not sure how to iterate through the data so that it separates each person's words correctly. I created a dummy file to create the word cloud, and I'm able to do that fine, so all I am wondering is how to extract the information I need.
Thank you! If there is more information I can offer please let me know. This is a beginning Python course.
EDIT: New code added from a response. This works to an extent, but only grabs the first line of each candidate's response, not their entire response. I need to write code that continues to store each line under that candidate until a new name is at the start of a line.
def getCandidate(myFile, candidate):
file = open(myFile, "r")
OBAMA = []
ROMNEY = []
LEHRER = []
file = file.readlines()
for line in file:
if line.startswith("OBAMA:"):
OBAMA.append(line)
if line.startswith("ROMNEY:"):
ROMNEY.append(line)
if line.startswith("LEHRER:"):
LEHRER.append(line)
if candidate == "OBAMA":
return OBAMA
if candidate == "ROMNEY":
return ROMNEY
EDIT: I now have a new question. How can I generalize the file so that I can open any debate file between two people and a moderator? I am having a lot of trouble with this one.
I've been given a hint to look at the beginning of the line and see if the last word of each line to see if it ends in ":", but I'm still not sure how to do this. I tried splitting each line on spaces and then looking at the first item in the line, but that's as far as I've gotten.
The hint is this: after you split your lines, iterate over them and check with the string function startswith for each candidate, then append.
The iteration over a file is very simple:
for row in file:
do_something_with_row
EDIT:
To keep putting the lines until you find a new candidate, you have to keep track with a variable of the last candidate seen and if you don't find any match at the beginning of the line, you stick with the same candidate as before.
if line.startswith('OBAMA'):
last_seen=OBAMA
OBAMA.append(line)
elif blah blah blah
else:
last_seen.append(line)
By the way, I would change the definitio of the function: instead of take the name of the candidate and returning only his lines, it would be better to return a dictionary with the candidate name as keys and their lines as values, so you wouldn't need to parse the file more than once. When you will work with bigger file this could be a lifesaver.