【问题标题】:Why does this dictionary generate like this?为什么这本词典会这样生成?
【发布时间】: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


    【解决方案1】:

    问题出在这里:

    weeks_count = [list(range(1, (weeks_to_sched + 1)))]
    

    它在列表内创建一个列表,因此当您迭代 weeks_count 时,您那里只有一个元素(整个范围的一个元素)。

    改为:

    weeks_count = list(range(1, (weeks_to_sched + 1)))
    

    请记住,在您的情况下,您不必将范围转换为列表(当您想要多次迭代范围时,转换为列表很有用),因此您可以直接使用它。

    weeks_count = range(1, (weeks_to_sched + 1))
    

    【讨论】:

      【解决方案2】:

      首先,您需要学习如何调试代码和查找错误。添加简单的打印行将帮助您发现问题

      print(weeks_count)
      print(x)
      
      #produces 
      [[1, 2, 3]]
      [1, 2, 3]
      

      这意味着x 等于整个数组而不是预期的int。那是因为您在列表中添加了额外的括号并使其成为数组,其中列表作为数组的第一个元素

          # not ok
          weeks_count = [list(range(1, (weeks_to_sched + 1)))]
          # ok
          weeks_count = list(range(1, (weeks_to_sched + 1)))
          # best
          weeks_count = range(1, weeks_to_sched + 1)
      

      但您也不需要保留 week_count 列表,因为您可以直接遍历范围(代码如下)

      我还建议不要丢失输入、一些全局 names 变量和将计划创建到 1 个函数中的逻辑。更好的方法是将所有必需的参数传递给这个函数,这样它就有一个责任

      最终代码和输出:

      def scheduleMaker(weeks_to_sched, names):
          schedule = {}
          for weekNum in range(1, weeks_to_sched + 1):
              schedule[f"Week {weekNum}"] = {}
              for j in names:
                  schedule[f"Week {weekNum}"][j] = [j]
          print(schedule)
      
      weeks_to_sched = int(input("How many weeks?"))
      scheduleMaker(weeks_to_sched, ['a', 'b', 'c'])
      
      {'Week 1': {'a': ['a'], 'b': ['b'], 'c': ['c']}, 'Week 2': {'a': ['a'], 'b': ['b'], 'c': ['c']}, 'Week 3': {'a': ['a'], 'b': ['b'], 'c': ['c']}}
      

      【讨论】:

        【解决方案3】:

        您需要使用range() 函数才能按照您描述的方式循环。

        尝试这样做

        def scheduleMaker():
            weeks_to_sched = int(input("How many weeks?"))
            schedule = {}
            #for every week of the season
            for x in range(1, weeks_to_sched + 1):
                #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)
        

        【讨论】:

        • 由于某种原因,作者从 1 开始计数周名称,所以他可能需要使用 range(1, weeks_to_sched + 1)
        • 同意,为了清楚起见,我将添加该编辑。谢谢! @VitaliyMoskalyuk
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 2018-06-08
        相关资源
        最近更新 更多