Question

Create a new list which increases the salary by 50% if the length of the person’s...

  1. Create a new list which increases the salary by 50% if the length of the person’s name is 3 characters or less or starts with 'S'. Print this list.

  2. Sort the list created in part e. by salary descending. Write this data to a CSV file called sorted2.csv

  3. Create a function that prints the elements of a list as defined in part e in CSV form. Test this function on the list you created in part e

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the remaining part, here is the last solution part of the question. Let me know for an y help with any other quesitons. thanks ======================================================================= data = data = " 2 Joe 95000 4 Steve 35000 1 Samantha 150000 10 Leah 99000 6 Riley 53215" # Use split to divide up the messy data where fields are separated by spaces. values = data.split() # Create individual lists for the ids, names, and salaries and print out each list. ids = [] names = [] salaries = [] for i in range(0, len(values), 3): ids.append(values[i]) names.append(values[i + 1]) salaries.append(int(values[i + 2])) # Write the data to a CSV file called cleaned.csv in the form: id, name, salary. filename = 'cleaned.csv' with open(filename, 'w+') as outfile: outfile.write('{},{},{}\n'.format('id', 'name', 'salary')) for i in range(len(ids)): outfile.write('{},{},{}\n'.format(ids[i], names[i], salaries[i])) # Create a new list where each element is a list representing a row in the CSV file. with open(filename, 'r') as infile: records = [] for line in infile.readlines()[1:]: line = [(val) for val in line.strip().split(',')] if len(line) == 3: records.append([int(line[0]), line[1], int(line[2])]) # Print this list with each record (i.e. element) on a new line. print(records) # Create and print a new list which includes only rows where the id >= 4 and id <= 8, or the person has a salary > 50000. filtered_list = [] for record in records: if 4 <= record[0] <= 8 and record[2] > 50000: filtered_list.append(record) filtered_list.sort(key=lambda record: record[0]) filename = 'sorted.csv' with open(filename, 'w+') as outfile: outfile.write('{},{},{}\n'.format('id', 'name', 'salary')) for record in filtered_list: outfile.write('{},{},{}\n'.format(record[0], record[1], record[2])) #Create a function that prints the elements of a list as defined in part e in CSV form. def print_list(aList): for record in aList: print(record) #Create a new list which increases the salary by 50% if the length of the person’s name is 3 characters or less or starts with 'S'. updated_list=[] for record in records: if len(record[1])<=3 or record[1][0]=='S': increment_salary = int(record[2]*1.5) updated_list.append([record[0],record[1],increment_salary]) updated_list.sort(key= lambda record:record[2],reverse=True) # Print this list. print_list(updated_list) filename = 'sorted2.csv' with open(filename, 'w+') as outfile: outfile.write('{},{},{}\n'.format('id', 'name', 'salary')) for record in updated_list: outfile.write('{},{},{}\n'.format(record[0], record[1], record[2])) print_list(updated_list)

=================================================================

Add a comment
Know the answer?
Add Answer to:
Create a new list which increases the salary by 50% if the length of the person’s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ** Language Used : Python ** PART 2 : Create a list of unique words This...

    ** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...

  • 1. Create a table that will hold the data in avgprice kwh state.csv. This .csv file...

    1. Create a table that will hold the data in avgprice kwh state.csv. This .csv file contains the annual data from 1990-2012 on the average energy price per kilowatt hour (KwH) by state and provider type. Implement the following queries: • Print each row that has Tennessee as state, order the result by year in descending order. • Print the average residential, commercial, industrial, and transportation price for the state of Texas from the year 1990-2012. 2. Create the tables...

  • In this assignment, you will create an application that holds a list of contact information. You ...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. MUST BE IN PYTHON FORMAT Create the folllowing objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

  • In this assignment, you will create an application that holds a list of contact information. You...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. Must Be In Python Format Create the following objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

  • Create a new program in Mu and save it as ps4.4.1.py and take the code below...

    Create a new program in Mu and save it as ps4.4.1.py and take the code below and fix it as indicated in the comments: # Write a function called "save_leaderboard" that accepts a # single list of tuples as a parameter. The tuples are of the # form (leader_name, score) # The function should open a file called "leaderboard.txt", # and write each of the tuples in the list to a line in the file. # The name and score...

  • Programming languages allow the programmer to create his or her own functions which are called __________...

    Programming languages allow the programmer to create his or her own functions which are called __________ __________ functions. The function that returns the number of characters in a string is the __________ function. __________ files contain records that must be processed in the order in which they were created. The elements of an array are stored in __________ storage locations in the computer’s memory. A variable that is declared outside all program modules, including the main module, has __________ scope....

  • This script will create a dictionary whose keys are all the directories listed in thePATH system...

    This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an ...

    I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an algorithm .. CSE 1311-Project 4 Part I: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows: 150 250 Anne Bob Ralph 305...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT