Question

In: Computer Science

python find min in the tuple of dictionaries by date also how to sort this tuple...

python find min in the tuple of dictionaries by date

also how to sort this tuple by effectiveDate

Requests = (

{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},

{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},

{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},

{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"}

)

Solutions

Expert Solution

Hi..

-------------------------------------------------------------

working code: find min by effectiveDate

Requests = (

{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},

{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},

{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},

{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"})
key_min = min(Requests,key=lambda x:x['effectiveDate'])
key_min

This is screenshot of above code and its output

------------------------------------------------------

Sort the dictionary by effectiveDate

Requests = (

{ "Id": 1, "itemId": 2, "amount": 50, "effectiveDate": "11/15/2015"},

{ "Id": 2, "itemId": 1, "amount": 200, "effectiveDate": "11/20/2015"},

{ "Id": 3, "itemId": 3, "amount": 5000, "effectiveDate": "10/5/2015"},

{ "Id": 4, "itemId": 3, "amount": 600, "effectiveDate": "10/6/2015"})
print("sorted List",sorted(Requests,key=lambda x:x['effectiveDate']))

This is screenshot of above code and its output

Still if you have any doubt or need any kind improvement, please let me know in comment section and if you like, Please upvote.

Thank You !


Related Solutions

Describe how tuples can be useful with loops over lists and dictionaries, and give Python code...
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source. Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
USE PYTHON to find out What day of the week is a given date? i.e. On...
USE PYTHON to find out What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is_leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing...
In python this structure can be represented by a set of tuples, where each tuple has...
In python this structure can be represented by a set of tuples, where each tuple has two elements. The following two lines would build the set given above and the print it. >>> L = [(’dog’,’white’), (’cat’,’black’),(’mouse’,’black’)] >>> f = set(L) >>> print(f) {(’cat’, ’black’), (’dog’, ’white’), (’mouse’, ’black’)} In the example, first we store the tuples into a list, and then we create a set with those tuples. There are obviously countless other ways to initialize f and get...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
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...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and Tyler Gary = { "name": "Gary", "homework": [90.0,97.0,75.0,92.0], "quizzes": [88.0,40.0,94.0], "tests": [75.0,90.0] } Alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } Tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } Insert one additional (new) dictionary of your choice Create a new list named students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT