I have a list containing multiple dictionaries. Each Dictionary will have a date and time Key. What I am trying to figure out is how print the values of each dictionary to a line in chronological order.
Below is an example of my code.
list_of_dicts = []
dict1 = {'Source': 'Log1', 'Type': 'Connection', 'Datetime': '2014-02-13 14:10:00', 'fullpath':'N/A'}
dict2 = {'Source': 'Log2', 'Type': 'Disconnect', 'Datetime': '2014-05-13 11:00:00', 'fullpath':'N/A'}
dict3 = {'Source': 'Log4', 'Type': 'Other', 'Datetime': '2014-05-10 02:50:00', 'fullpath':'N/A'}
list_of_dicts.append(dict1)
list_of_dicts.append(dict2)
list_of_dicts.append(dict3)
The expected output would look like this:
Datetime Source Type Fullpath
2014-02-13 14:10:00 Log1 Connection N/A
2014-05-10 02:50:00 Log4 Other N/A
2014-05-13 11:00:00 Log2 Disconnect N/A
I would greatly appreciate anyone's guidance on this. Thanks so much.
Your dates are formatted using the ISO8601 format, making them lexicographically sortable.
Just sort your list on the Datetime
key of each dictionary:
from operator import itemgetter
for entry in sorted(list_of_dicts, key=itemgetter('Datetime')):
# format your output
Demo:
>>> list_of_dicts = [
... {'Source': 'Log1', 'Type': 'Connection', 'Datetime': '2014-02-13 14:10:00', 'fullpath':'N/A'},
... {'Source': 'Log2', 'Type': 'Disconnect', 'Datetime': '2014-05-13 11:00:00', 'fullpath':'N/A'},
... {'Source': 'Log4', 'Type': 'Other', 'Datetime': '2014-05-10 02:50:00', 'fullpath':'N/A'},
... ]
>>> from operator import itemgetter
>>> for entry in sorted(list_of_dicts, key=itemgetter('Datetime')):
... print entry
...
{'Source': 'Log1', 'fullpath': 'N/A', 'Type': 'Connection', 'Datetime': '2014-02-13 14:10:00'}
{'Source': 'Log4', 'fullpath': 'N/A', 'Type': 'Other', 'Datetime': '2014-05-10 02:50:00'}
{'Source': 'Log2', 'fullpath': 'N/A', 'Type': 'Disconnect', 'Datetime': '2014-05-13 11:00:00'}
Use http://docs.python.org/2/library/functions.html#sorted with a lambda function. It works for more deeply nested dictionaries as well.
sorted_entries = sorted(list_of_dicts,key=lambda x:x['Datetime'])
I did not take the time to format things pretty for you, but here is a quick and dirty way to sort these. Since your dates are formatted in a nice way, they can be sorted easily. This is not as efficient as the other answers, because I am constructing a whole new dictionary with redundant data.
list_of_dicts = []
dict1 = {'Source': 'Log1', 'Type': 'Connection', 'Datetime': '2014-02-13 14:10:00', 'fullpath':'N/A'}
dict2 = {'Source': 'Log2', 'Type': 'Disconnect', 'Datetime': '2014-05-13 11:00:00', 'fullpath':'N/A'}
dict3 = {'Source': 'Log4', 'Type': 'Other', 'Datetime': '2014-05-10 02:50:00', 'fullpath':'N/A'}
list_of_dicts.append(dict1)
list_of_dicts.append(dict2)
list_of_dicts.append(dict3)
d = {d['Datetime']:d for d in list_of_dicts} # dictionary comprehension
for k in sorted(d.keys()):
print(d[k])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.