【发布时间】:2021-11-03 19:07:26
【问题描述】:
我正在制作一个简单的应用程序来安排联赛比赛。我知道比赛的周数,那一周的每支球队都需要一场比赛。我还没说完,但我正在测试它,输出会很奇怪。
def scheduleMaker(dicty):
weeks_to_sched = int(input("How many weeks?"))
#weeks count
weeks_count = [list(range(1, (weeks_to_sched + 1)))]
schedule = {}
#for every week of the season
for x in weeks_count:
#add a entry in the dict for this week
schedule[f"Week {x}"] = {}
#every team that week needs a matchup
for j in names:
#this team needs to be added to week 1 with a matchup
schedule[f"Week {x}"][j] = [j]
print(schedule)
scheduleMaker(teams)
我想要为每周创建一个字典,然后是每周字典中的键:值(匹配项)。我在代码的第一行遇到问题。我得到:
{'Week [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]': {}
代替:
{'Week 1': {}, 'Week 2': {}, 'Week 3: {}', etc}
【问题讨论】:
标签: python list dictionary generator