简体   繁体   中英

iterate over a Python tuple

I am trying to create a Python scheduler where I am trying to schedule a meeting on a particular day.

Here is the code:

timetable = [[""] * 24 for slots in range(7)]

WEEKDAYS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',     
'Saturday', 'Sunday')

timetable[0][15] = "meeting with Jane"

for day in timetable:
  for i, event in enumerate(day):
    if event:
      print("%s at %02d:00 -- %s" % (WEEKDAYS[day], i, event))

However when I run the above code I get an error;

Traceback (most recent call last):
  File "C:\Users\workspace\test\2D.py", line 18, in <module>
    print("%s at %02d:00 -- %s" % (WEEKDAYS[day], i, event))
TypeError: tuple indices must be integers, not list

I am trying to print the day when the meeting was scheduled for eg: Monday meeting with Jane or Saturday meeting with Jane.

How do i print the exact day as well?

OK, the problem is that in WEEKDAYS[day] , day is a list of 24 events; you want to use enumerate() again:

timetable = [[""] * 24 for slots in range(7)]

WEEKDAYS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday')

timetable[0][15] = "meeting with Jane"

for j, day in enumerate(timetable):
    # day is a list of 24 entries
    for i, event in enumerate(day):
        if event:
            print("%s at %02d:00 -- %s" % (WEEKDAYS[j], i, event))

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