Question

In: Computer Science

How can I get the days to print in order (Monday, Tuesday, Wednesday etc) def summarize_by_weekday(expense_list):...

How can I get the days to print in order (Monday, Tuesday, Wednesday etc)

def summarize_by_weekday(expense_list):
"""
Requirement 3 to display the total amount of money spent on each weekday, aggregated per day.
That is, display “Monday: $35”, “Tuesday: $54”, etc., where $35 is the sum of dollar amounts for a
ll Mondays present in the data file, $54 is the sum of dollar amounts for all Tuesdays present in the data file,
and so on.
:param expense_list:
:return: None
"""
  weekday_spent_dict={}
for expense in expense_list:
if weekday_spent_dict.get(expense[0])==None:
weekday_spent_dict[expense[0]]=expense[1]
else:
weekday_spent_dict[expense[0]] += expense[1]
for day in sorted(weekday_spent_dict.keys()):
print('{0}: ${1:5.2f}'.format(day,weekday_spent_dict.get(day)))

output is:

Enter 1 display all of the expense records
2 to display all of the expense records for a particular category
3 to display the total amount of money spent on each weekday, aggregated per day
0 to quit.3
Friday: $94.90
Monday: $40.75
Saturday: $201.38
Sunday: $305.44
Thursday: $276.49
Tuesday: $14.85
Wednesday: $14.85

I want it to print:

Monday

Tuesday

Wednesday

etc..

def get_data(fname):
fileObj = open(fname)
lines = fileObj.readlines()
expense_list = []

for line in lines[1:]: # for lines starting at 2 (excluding 1 because it is the
line_items = line.strip().split(",") # strip removes the newline character
expense_list.append([line_items[0], float(line_items[1]), line_items[2]])

return expense_list

Solutions

Expert Solution

def summarize_by_weekday(expense_list):
        """
        Requirement 3 to display the total amount of money spent on each weekday, aggregated per day.
        That is, display “Monday: $35”, “Tuesday: $54”, etc., where $35 is the sum of dollar amounts for all Mondays present in the data file, $54 is the sum of dollar amounts for all Tuesdays present in the data file,
        and so on.
        :param expense_list:
        :return: None
        """
        weekday_spent_dict={}
        for expense in expense_list:
                if weekday_spent_dict.get(expense[0])==None:
                        weekday_spent_dict[expense[0]]=expense[1]
                else:
                        weekday_spent_dict[expense[0]] += expense[1]

        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        for day in days:
                if day in weekday_spent_dict:
                        print('{0}: ${1:5.2f}'.format(day, weekday_spent_dict.get(day)))
**************************************************
You can define the ordering of days in a list, and then check the data for each day because monday tuesday etc do not have a ordering on basis of their english names.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

The table below shows the price of petrol for a week. Day Monday Tuesday Wednesday Thursday...
The table below shows the price of petrol for a week. Day Monday Tuesday Wednesday Thursday Friday Saturday Sunday Price of Petrol (per Liter) 1.57 1.85 1.86 1.94 1.49 1.66 1.69 a) Using a 4-day Moving Average to forecast the petrol price on Saturday, and calculate the forecast error. b) Given the exponential smoothing constant ? = 0.3, the petrol price on Saturday, and the forecast petrol price on Saturday (from (a)), forecast the petrol price on Sunday by using...
A store sold 12 stereos on Monday, 17 on Tuesday, 28 on Wednesday, 17 on Thursday...
A store sold 12 stereos on Monday, 17 on Tuesday, 28 on Wednesday, 17 on Thursday and 26 on Friday. AT the .01 level, test if there is a difference in the number of stereos sold on each weekday. State the hypotheses and identify the claim, find the critical value(s), compute the test value, make the decision and summarize the results. Show all work and formulas - sample question that I don't get.
I can't get the number of days to print. Here is my code: public static void...
I can't get the number of days to print. Here is my code: public static void main(String[] args) { // Prompt the user to enter year Scanner scanner = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2016): "); int year = scanner.nextInt(); for(int i = 1; i <= 12; i++) printMonth(year, i); } /** Print the calendar for a month in a year */ static void printMonth(int year, int month) { // Print the...
The daily requirements for patrol officers are: Monday:4 officers Tuesday:5 officers Wednesday:6 officers Thursday:7 Officer Friday:10...
The daily requirements for patrol officers are: Monday:4 officers Tuesday:5 officers Wednesday:6 officers Thursday:7 Officer Friday:10 Officers Saturday:9 officers Sunday:9 officers Question: Each officer works 5 days a week and must have 2 consecutive days off. You are scheduling officers to work the appropriate days of the week. The second officer you schedule is to work which days of the week?
I can not get my Group Statistics or Indepependent Sample test to print...keep saying one group...
I can not get my Group Statistics or Indepependent Sample test to print...keep saying one group info is missing but it shows on other reports. How is the information entered on the SPSS grid. I want to see if I am enterring something wrong or it may be the software I just purchased 2 days ago. For Problem set 1 and 2 The independent-samples t-test. show each entry for both 1 and 2. Thanks
"Please can I get a feedback on this discussion post below" Can I get it in...
"Please can I get a feedback on this discussion post below" Can I get it in 2 hours please .thanks Tesla is a company recently in the news for a conflict of interest between the SEC and the CEO Elon Musk due to an interest to bring the company private by the CEO who shared on Twitter his short term plans. Production issues, a number of layoffs and the increasing demand for the electric vehicles were expressed by the CEO...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
I have a sequence of natural numbers. How can I get the height of the resulting...
I have a sequence of natural numbers. How can I get the height of the resulting tree/ c++(send code)
How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT