List with people names - python

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

Related

How do i sort a class to match specific characteristics

So,Basically im new to python and coding overall.Starting to build my first project.I learned some courses for OOP but i need some help.
My idea for a project is to show specific kind of mushrooms based on the user input like cap color,gills color,habitat and etc.
Do i work with classes or i need to start learning database.How exacly i am able to accomplish my goal.
An example of this will be
Input is:Yellow,white,forest
Output need to be list of mushrooms that have the same characteristics.
I know how do classes work but i seem to be hitting a wall here
I recommend you to use dictionaries as well as read the documentation of dictionaries and lists in python, but here is an idea:
mushrooms_college = {'name':['Ganoderma lucidum','Amanita phalloides'],'color':['red and yellow', 'red and black']}
color_mushroom = input('what is the color? ')
values_colors_mushrooms = list(mushrooms_college['color'])
index = values_colors_mushrooms .index(color_mushroom)
mushrooms_college['name'][index]

Google Classroom Python make return course materials aswell

Ok so basically, I'm trying to get all the course work there are for a course. But i only get in response the coursework that are not of type 'MATERIAL'. How can I do so that I get the coursework type 'MATERIAL' aswell?
Here is my python code:
resultcourses = service.courses().list().execute()
courses = resultcourses.get('courses', [])
for course in courses:
resultscourseworks = service.courses().courseWork().list(courseWorkStates='PUBLISHED',
courseId=course['id']).execute()
courseworks = courseworks + resultscourseworks.get('courseWork', [])```
Currently it is not possible to get the Material coursework as mentioned on the first paragraph of the Manage Classwork guide:
Classroom has five types of Classwork: Assignments, Quiz Assignments,
Questions (short answer and multiple choice), Materials, and Topics.
The Classroom API currently allows developers to read and write two of
these types: Assignments and Questions.

Is there are correct way to print messages using list entities?

I want to know if there's a "correct" or "preferred" way to print a message using list entities.
I'm currently working through Python Crash Course 2ed and I'm at Exercise 3-4 which asks:
If you could invite anyone, living or deceased, to dinner, who would you invite? Make a list that includes at least three people you’d like to invite to dinner. Then use your list to print a message to each person, inviting them to dinner.
The code I wrote gets the job done, but it differs from the solution found online.
My code:
guests = ['abe lincoln', 'jesus', 'jack nicholson']
print(f"Hello, {guests[0].title()}, would you like to come to dinner?")
print(f"Hello, {guests[1].title()}, would you like to come to dinner?")
print(f"Hello, {guests[2].title()}, would you like to come to dinner?")
The book solution:
guests = ['guido van rossum', 'jack turner', 'lynn hill']
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
They both produce the desired results, but is one form better to use than the other?
When using lists or other iterable types, most preferred way to to loop through them, e.g:
guests = ['abe lincoln', 'jesus', 'jack nicholson']
for i in range(len(guests)):
print(f"Hello, {guests[i].title()}, would you like to come to dinner?")
Also, in Python, the for loop can simple be used as:
for guest in guests:
print(f"Hello, {guest.title()}, would you like to come to dinner?")
In one line:
for guest in guests : print(f"Hello, {guest.title()}, would you like to come to dinner?")
Bonus:
print(f"{0}".format(argument)) can be also used to pass argument in the print function in an ordered way. Example:
print(f"Hello, {0}, would you like to come to dinner?".format(guest.title()))

match hex string with list indice

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.'

Creating a load profile function in python

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.

Categories