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.
Python Question I have created a dictionary shown below: import pandas as pd records_dict = {'FirstName':...
Python Question I have created a dictionary shown below: import pandas as pd records_dict = {'FirstName': [ 'Jim', 'John', 'Helen'], 'LastName': [ 'Robertson', 'Adams', 'Cooper'], 'Zipcode': [ '21801', '22321-1143', 'edskd-2134'], 'Phone': [ '555-555-5555', '4444444444', '323232'] } I have stored this dictionary in a data frame, like shown below: records = pd.DataFrame(records_dict) print(records) I am able to print the records just fine. My issue is, I want to eliminate, or put a blank space in, the values of the zipcode and...
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...
here i have a dictionary with the words and i have to find the words with...
here i have a dictionary with the words and i have to find the words with the largest size, (i mean word count for eg abdominohysterectomy) which have anagrams , like for example ,loop -> pool. Also , i have to sort them in alphabetical way so what could be the algorithm for that public class Anagrams {    private static final int MAX_WORD_LENGTH = 25;    private static IArray<IVector<String>> theDictionaries =        new Array<>(MAX_WORD_LENGTH);    public static void...
(Python) I want to use a function called level() that takes a dictionary. Here is a...
(Python) I want to use a function called level() that takes a dictionary. Here is a dictionary with people's job and skill level. dict1 = {'Jame': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},Pam': {'Plumbing': 2, 'Cleaning': 5}) like if I called level(dict1), the output will return {'Pam', 'James'} It finds the people's average skill level like for Pam is (2+5)/2=3.5 and sorted descending. How do I do that and how do I do it in only one return statement(using comprehension or...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT