Question: Write a two-part program. The first part builds a list. The second part uses the list to build a dictionary holding the same information. Both






Write a two-part program. The first part builds a list. The second part uses the list to build a dictionary holding the same information. Both parts should be in the same file. List-making This is just an exercise in manipulating a list, with no input from the user. You need to build a "list of lists" that is, a list of 7 elements, one per day of the week, where each element is itself a list of that day's activities. Use the Python "pprint" module to "pretty-print" your result list. 1. First construct a list of names of the days of the week. What day does the week start on? In the U.S. most people start the week on Sunday, but some people start on Monday. (And it could start any day you wish, really.) Choose which day you wish your week to start with this will be element 0 of the list of weekday names. 2. Next construct a list of weekday activities, where list element is a sub-list activities. Initially each sub-list should have four elements: 'breakfast', 'lunch', 'NOTHING', 'supper'. 3. Use a for loop to print out: each index value; each weekday name, and the corresponding activities list. The loop could look like this: for index in range( 7 ): print("%d %s %s' % (index, daynames[index], activities [index])) The instructor's output looks like this: 3 $ python3 week-schedule.py O Monday ['breakfast', 'lunch', 'NOTHING', 'supper'] 1 Tuesday '['breakfast! 'lunch 'NOTHING 'supper'] 2 Wednesday ['breakfast', 'lunch', 'NOTHING' 'supper'] 3 Thursday ['breakfast' 'lunch 'NOTHING' 'supper'] 4 Friday ['breakfast', 'lunch NOTHING', supper'] 5 Saturday ['breakfast' 'lunch', 'NOTHING' supper'] 6 Sunday ['breakfast', 'lunch', 'NOTHING', 'supper'] Observe that it is one list with seven elements, and each element is itself a list of four elements. 4. Now modify each of the sub-lists by adding more activities. Use the .index(), .append(), .insert(), .pop(), and .remove() methods as needed to modify each list. Get rid of the 'NOTHING' element (or move it to its correct location if you have a lazy period in your schedule :-), and add at least your classes. Add other items as you see fit. Here are some examples, as run in Jupyter: In [43]: print(activities [0]) ['breakfast', 'lunch', 'NOTHING', 'supper'] In [44] posn = activities [0].index('NOTHING') In [45]: activities [0] .pop (posn) Out[45]: 'NOTHING' In [46]: posn = activities[0].index('lunch') In [47]: activities[0].insert( (posn+1), 'English class' ) In [48]: print(activities [0]) ['breakfast', 'lunch', 'English class', 'supper'] In [49] 5. Print out your lists again, this time showing the added activities for each day. An example of the instructor's schedule looks like this: 0 Monday ['gym', 'breakfast', 'research', 'lunch' 'office hour' 275-01', supper', 'LC meeting'] Tuesday '['breakfast', '115-03 'office hour' 240-01 '240-02' 'research 2 Wednesday' ['gym' 'breakfast 'research' lunch 'office hour 275-01", supper) Thursday ['breakfast' '115-03: 'office hour' "240-01 *240-02'. research ; 'supper', 'dept. meeting', 'supper' Friday ['breakfast' doctor 'office work', 'supper'] Saturday ['breakfast' lunch supper'] 6 Sunday ['breakfast, lunch', supper'] 'suppering' acm' lunch Dictionary-making 1. Next construct an empty dictionary. Then add key:value pairs to it. Each key is a day of the week; each value is the appropriate day's list from your activities list. The result can be called a "dictionary of lists". 2. Import the pprint module. Then print out your dictionary, using "pprint.pprint()". As you can see, there is no longer any need to identify the day of the week, since that information is in each element's key. On the other hand, the days may not be in the right order. There are ways to handle this, but don't worry about it for now. An example of the instructor's schedule, as printed using "pprint.pprint()", looks like this: {'Friday': ['breakfast', 'doctor', 'lunch', 'office work', 'supper'], Monday': (gym', 'breakfast', 'research', 'lunch', 'office hour', 275-01', supper' 'LC meeting'], Saturday': ['breakfast 'lunch', 'NOTHING', 'supper'], Sunday': ['breakfast', lunch', 'NOTHING', 'supper'], 'Thursday': ['breakfast', '115-03', 'office hour', '240-01', '240-02', researc', 'acm', dept. meeting', supper'], ', '115-03', 'office hour' 240-01', 240-02 research', 'acm', supper'], 'Wednesday': ['gym', 'breakfast', 'research', 'lunch', 'office hour', '275-01' 'supper')} Turn it in Submit a single program that first creates the list of lists" and then displays it; and then creates the "dictionary of lists" and displays that
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
