156k views
3 votes
1. Open the `comprehensions.py` file

2. Implement the `to_mod_list()` function by using the `map()` function to apply `mod()`
to all elements within `employee_list`.
Assign the result of it to a new variable called `map_emp`. Convert `map_emp` to a list and return it.
- The `mod()` function returns a string value for example such as `"Lisa_Cold Storage"` from the dictionary passed to it.


3. At this point you should have a list of the values such as: `"Lisa_Cold Storage"` mentioned above.
But that is no good for a username with the whitespace present in it.
- Implement the `generate_usernames()` method by using list comprehension and the `replace()` over `mod_list`
to replace all spaces `(" ")` with underscores `("_")`.
- Return the resulting list.


4. We want to create a dictionary that stores employees' first initials and IDs.
- Implement `map_id_to_initial()` by using dictionary
comprehension over the `employee_list` to create a dictionary
where each key is the
first letter of an employee's name and the value is the employee's ID.


5. Run the script by opening the terminal and executing the command:
```
python3 comprehensions.py
```


comprehensions.py file:
# Input data: List of dictionaries
employee_list = [
{"id": 12345, "name": "John", "department": "Kitchen"},
{"id": 12456, "name": "Paul", "department": "House Floor"},
{"id": 12478, "name": "Sarah", "department": "Management"},
{"id": 12434, "name": "Lisa", "department": "Cold Storage"},
{"id": 12483, "name": "Ryan", "department": "Inventory Mgmt"},
{"id": 12419, "name": "Gill", "department": "Cashier"}
]
# Function to be passed to the map() function. Do not change this.
def mod(employee_list):
temp = employee_list['name'] + "_" + employee_list["department"]
return temp
def to_mod_list(employee_list):
""" Modifies the employee list of dictionaries into list of employee-department strings
[IMPLEMENT ME]
1. Use the map() method to apply mod() to all elements in employee_list
Args:
employee_list: list of employee objects
Returns:
list - A list of strings consisting of name + department.
"""
### WRITE SOLUTION CODE HERE
raise NotImplementedError()
def generate_usernames(mod_list):
""" Generates a list of usernames
[IMPLEMENT ME]
1. Use list comprehension and the replace() function to replace space
characters with underscores
List comprehension looks like:
list = [ function() for in ]
The format for the replace() function is:
test_str.replace("a", "z") # replaces every "a" in test_str with "z"
Args:
mod_list: list of employee-department strings
Returns:
list - A list of usernames consisting of name + department delimited by underscores.
"""
### WRITE SOLUTION CODE HERE
raise NotImplementedError()
def map_id_to_initial(employee_list):
""" Maps employee id to first initial
[IMPLEMENT ME]
1. Use dictionary comprehension to map each employee's id (value) to the first letter in their name (key)
Dictionary comprehension looks like:
dict = { key : value for in }
Args:
employee_list: list of employee objects
Returns:
dict - A dictionary mapping an employee's id (value) to their first initial (key).
"""
### WRITE SOLUTION CODE HERE
raise NotImplementedError()
def main():
mod_emp_list = to_mod_list(employee_list)
print("Modified employee list: " + str(mod_emp_list) + "\\")
print(f"List of usernames: {generate_usernames(mod_emp_list)}\\")
print(f"Initials and ids: {map_id_to_initial(employee_list)}")
if __name__ == "__main__":
main()
In python please!

User Clp
by
7.7k points

1 Answer

4 votes

Below is the code for the completed comprehensions.py file:

Python

# Input data: List of dictionaries

employee_list = [{"id": 12345, "name": "John", "department": "Kitchen"},

{"id": 12456, "name": "Paul", "department": "House Floor"},

{"id": 12478, "name": "Sarah", "department": "Management"},

{"id": 12434, "name": "Lisa", "department": "Cold Storage"},

{"id": 12483, "name": "Ryan", "department": "Inventory Mgmt"},

{"id": 12419, "name": "Gill", "department": "Cashier"}]

# Function to be passed to the map() function. Do not change this.

def

mod(employee_list):

temp = employee_list['name'] + "_" + employee_list["department"]

return temp

def

to_mod_list(employee_list):

""" Modifies the employee list of dictionaries into list of employee-department strings

[IMPLEMENT ME]

1. Use the map() method to apply mod() to all elements in employee_list

Args:

employee_list: list of employee objects

Returns:

list - A list of strings consisting of name + department.

"""

# WRITE SOLUTION CODE HERE

map_emp = map(mod, employee_list)

mod_emp_list = list(map_emp)

return mod_emp_list

def

generate_usernames(mod_list):

""" Generates a list of usernames

[IMPLEMENT ME]

1. Use list comprehension and the replace() function to replace space

characters with underscores

List comprehension looks like:

list = [ function() for in ]

The format for the replace() function is:

test_str.replace("a", "z") # replaces every "a" in test_str with "z"

Args:

mod_list: list of employee-department strings

Returns:

list - A list of usernames consisting of name + department delimited by underscores.

"""

# WRITE SOLUTION CODE HERE

usernames = [x.replace(" ", "_") for x in mod_list]

return usernames

def map_id_to_initial(employee_list):

""" Maps employee id to first initial

[IMPLEMENT ME]

1. Use dictionary comprehension to map each employee's id (value) to the first letter in their name (key)

Dictionary comprehension looks like:

dict = { key : value for in }

Args:

employee_list: list of employee objects

Returns:

dict - A dictionary mapping an employee's id (value) to their first initial (key).

"""

# WRITE SOLUTION CODE HERE

id_to_initial = {employee["id"]: employee["name"][0] for employee in employee_list}

return id_to_initial

def main():

mod_emp_list = to_mod_list(employee_list)

print("Modified employee list: " + str(mod_emp_list) + "\\")

print(f"List of usernames: {generate_usernames(mod_emp_list)}\\")

print(f"Initials and ids: {map_id_to_initial(employee_list)}")

if __name__ == "__main__":

main()

User Study
by
8.1k points