Question

In: Computer Science

I have a python dictionary with the following format Key Type Value January str ['January 01...

I have a python dictionary with the following format

Key Type Value

January str ['January 01 2020', 'January 02 2019', 'January 03 2018']

June str ['June 04 2018', 'June 05 2018', 'June 06 2016]

August str ['Augsut 07 2016', 'August 08 2016']

How do return the following conclusion with python code?

January has the most day (1) in 2020

January has the most day (1) in 2019

June has the most days (2) in 2018

August has the most days (2) in 2016

Solutions

Expert Solution

In case of any query do comment. Thanks

Code:

#function to calculate number of days in the given dictionary for the given month as key and year to serach
def findNumbeOfDays(monthsDictionary, key, year):
    count =0
    #if month is present in the dictionary
    if key in monthsDictionary:
        #take out the value part which is a list here
        listValue = monthsDictionary[key]
        #iterate over a list of values
        for item in listValue:
            #find if item in the list contains year given then increase the count
            if year in item:
                count = count +1
    return count


def main():
    #prepare a dictionary as given
    months ={
                'January':['January 01 2020', 'January 02 2019', 'January 03 2018'],
                'June':['June 04 2018', 'June 05 2018', 'June 06 2016'] ,
                'August': ['August 07 2016', 'August 08 2016']
                
            }
    #call findNumbeOfDays function as per the given format print the output
    print("January has the most day ({}) in 2020".format(findNumbeOfDays(months,'January','2020')))
    print("January has the most day ({}) in 2019".format(findNumbeOfDays(months,'January','2019')))
    print("June has the most day ({}) in 2018".format(findNumbeOfDays(months,'June','2018')))
    print("August has the most day ({}) in 2016".format(findNumbeOfDays(months,'August','2016')))
#main driver function
if __name__ == '__main__':
    main()

screen shot of the code for indentation and output:


Related Solutions

in Python 3, I need to search a dictionary for the matching key while ignoring case...
in Python 3, I need to search a dictionary for the matching key while ignoring case of the string and the key. then print the dictionary item in it's original case. here is the code I have so far but the compiler isn't liking that the name string is lowercase ``` dictionary = {"Dan Smith": {"street":"285 Andover Lane", "city":"Pompano Beach", "state":"FL", "zip":"33060"},"Parker Brady": {"street":"7416 Monroe Ave.", "city":"Windsor", "state":"CT", "zip":"07302"}} name ="dan smith" line= dict(filter(lambda item: name.lower() in item[0], dictionary.items())) print("Street:%5s...
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
For the following MIPS instructions, (1) show its format or type (I-format, R- format, or J-...
For the following MIPS instructions, (1) show its format or type (I-format, R- format, or J- format); (2) translate them into binary using the follow procedure (Note: $s0-$s7 are the 16-23th registers, $t0-$t7 are the 8-15th registers). For example, addi    $s0, $s1, 2               # the op code of addi is 0010002 Format (?): lw       $s1, 4($s1)               # the op code of lw is 1000112 Format (?): bne     $s1, $t1, loop           # the value of loop here is...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
How do I make a dictionary in Python language? Can somebody please provide me with an...
How do I make a dictionary in Python language? Can somebody please provide me with an example code of a Dictionary in Python Language? Thank you in advance.
On Python a) Use format() method to print an integer value entered by the user and...
On Python a) Use format() method to print an integer value entered by the user and its cube root with two decimal places. b) Print the same values as part (a) using format() function with keyword arguments and labels number and cubeRoot as in: format(number=n,cubeRoot=cr) c) Switch the order of keyword arguments and show that this has no effect on the output.
Process each line so you create dictionary where key is name of the company and value...
Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by...
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE)...
Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE) pair. 2.1) Create an empty dictionary called 'contacts'. The KEY in this dictionary will be the name of a contact (stored as a string). The VALUE in this dictionary will be a list with three elements: the first element will be a phone number (stored as a string), the second an email address (stored as a string) and the third their age (stored as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT