繁体   English   中英

迭代一个 Python 元组

[英]iterate over a Python tuple

我正在尝试创建一个 Python 调度程序,我试图在特定日期安排会议。

这是代码:

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))

但是,当我运行上述代码时,出现错误;

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

我正在尝试打印安排会议的日期,例如:周一与 Jane 会面或周六与 Jane 会面。

我如何打印确切的日期?

好的,问题是在WEEKDAYS[day]day是 24 个事件的列表; 你想再次使用enumerate()

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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