简体   繁体   中英

How to rearrange a dict keys in increasing order (ex: date in a year as key)? python 3

Ive been working with dictionaries recently and Ive been working on a project which includes saving events of a day to a calendar.

so the format for the key would be in the date format yyyy-mm-dd. I wanted to make it so that every 'key' that is in the dictionary would come out as according to the days. For example:the key 2015-11-10 with its events on that day and 2015-11-9 with its events, I would like 2015-11-9 and its events to be printed out first. I have tried sorting and tuples etc.. but that would only sort what is inside the key not the key itself. Any ideas or suggestions?

>>> calendar = {}
    >>> command_add("2015-10-12", "Eye doctor", calendar)
    ''
    >>> command_add("2015-10-12", "lunch with sid", calendar)
    ''
    >>> command_add("2015-10-29", "Change oil in blue car", calendar)
    ''
    >>> command_add("2015-10-12", "dinner with Jane", calendar)
    ''
    >>> command_add("2015-10-29", "Fix tree near front walkway", calendar)
    ''
    >>> command_add("2015-10-29", "Get salad stuff", calendar)
    ''
    >>> command_add("2015-11-06", "Sid's birthday", calendar)
    ''
    >>> command_show(calendar)

What I need is this:

>>> command_show(calendar)
        2015-10-12:
            0: Eye doctor
            1: lunch with sid
            2: dinner with Jane
        2015-10-29:
            0: Change oil in blue car
            1: Fix tree near front walkway
            2: Get salad stuff
        2015-11-06:
            0: Sid's birthday
    ''

Normal dictionaries are unordered. If you want an ordered dictionary, you have to use OrderedDict .

But I guess it would be enough to iterate over the values in an ordered fashion. Usually, you do it like this:

for key, value in sorted(mydict.items()):
    pass  # now it is sorted in ascending order of key

Please note that mydict is still unordered. .items() returns a list of (key, value) tuples. And lists can be sorted.

dict with list elements

#!/usr/bin/env python


def command_add(day, event_name, calendar):
    if day in calendar:
        calendar[day].append(event_name)
    else:
        calendar[day] = [event_name]


def command_show(calendar):
    """
    Parameters
    ----------
    calendar : dict
        The keys are dates in the format YYYY-MM-DD and the values are lists
        of events
    """
    for day, events in sorted(calendar.items()):
        print("%s:" % day)
        for i, event in enumerate(events):
            print("\t%i: %s" % (i, event))


calendar = {}
command_add("2015-10-12", "Eye doctor", calendar)
command_add("2015-10-12", "lunch with sid", calendar)
command_add("2015-10-29", "Change oil in blue car", calendar)
command_add("2015-10-12", "dinner with Jane", calendar)
command_add("2015-10-29", "Fix tree near front walkway", calendar)
command_add("2015-10-29", "Get salad stuff", calendar)
command_add("2015-11-06", "Sid's birthday", calendar)
command_show(calendar)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM