List enumeration in a list

I have a date with events that occurred on a date. I want to list the events on the day I show the calendar.

I also need to be able to remove the event from the list.

def command_add(date, event, calendar):
    if date not in calendar:
        calendar[date] = list()
    calendar[date].append(event)


calendar = {}
command_add("2015-10-29", "Python class", 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)
print(calendar)

def command_show(calendar):
    for (date, event) in calendar:
       print(date, enumerate(event))

command_show(calendar)

I thought this would allow me to access the secondary list under the date and list it, but I get an error.

Example:

command_show(calendar)
    2015-10-20:
        0: stackover flow sign up
    2015-11-01:
        0: stackoverflow post
        1: banned from stack overflow
+4
source share
3 answers

Just change your function command_show()to this, if you are not using dict.items(), then you will only get the keys (not both keys and values):

def command_show(calendar):
    for (date, event) in calendar.items():
        print(date+':')
        for i in enumerate(event):
            print('    '+str(i[0])+': '+i[1])

Output:

2015-10-29:
    0: Python class
    1: Change oil in blue car
2015-10-12:
    0: Eye doctor
    1: lunch with sid

About why I do this:

for i in enumerate(event):
    print('    '+str(i[0])+': '+i[1])

As you can see, I use enumerate()here. From the doc:

. iterable , , .
__next__() , enumerate(), , ( , 0), , iterable.

, - [(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')], evernt - ['Python class', 'Eye doctor', 'lunch with sid'].

[(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')], for , for i in enumerate(event), i (0, 'Python class') , (1, 'Eye doctor') ..

, - 0: Python class ( ), , ' '+ (+ , , 'foo'+'bar' - foobar).

, i , slice. i[0] , i[1] ..

i[0] , - 0 + 'foobar' ( TypeError: unsupported operand type(s) for +: 'int' and 'str'). str(), . ... , .


- :

for num, event in enumerate(event):
    print('    '+str(num), event, sep=': ')

? for num, event in enumerate(event) - num = 0, evert = 'Python class' ... .

sep .

+2

for ... in calendar: . .items(), :

def command_show(calendar):
    for date, events in calendar.items():
        print(date, enumerate(events))

command_add .setdefault():

def command_add(date, event, calendar):
    calendar.setdefault(date, []).append(event)
+4

You can use defaultdictone that takes care of this iffor you; and pass through calendar.iteritems()so that you get a tuple key,valueat each iteration.

from collections import defaultdict

calendar = defaultdict(list)

def command_add(date, event, calendar):
    calendar[date].append(event)

command_add("2015-10-29", "Python class", 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)
print(calendar)

for date, events in calendar.iteritems():
    print(date)
    for event in events:
        print('\t{}'.format(event))
0
source

Source: https://habr.com/ru/post/1614010/


All Articles