【问题标题】:Why would you assign a variable to ""?为什么要将变量分配给“”?
【发布时间】:2016-02-10 22:31:33
【问题描述】:

所以我在 Treehouse 网站上的 Python 课程中间,问题正是这样问的:

创建一个名为 most_classes 的函数,该函数接受教师词典。每个键都是老师的名字,它们的值是他们教过的课程的列表。 most_classes 应该返回班级最多的老师。

在这里,我在下面发布了从 Treehouse 论坛上的资源中找到的正确代码,我也提出了同样的问题,但没有得到答复 - 那么分配教师 =“”到底是做什么的?我很困惑

 # The dictionary will be something like:
 # {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
 #  'Kenneth Love': ['Python Basics', 'Python Collections']}

 # Often, it's a good idea to hold onto a max_count variable.
 # Update it when you find a teacher with more classes than
 # the current count. Better hold onto the teacher name somewhere
 # too!

def most_classes(my_dict):
    count = 0
    teacher = "" #this is where I am confused!
    for key in my_dict: 
        if(len(my_dict[key]) > count):
            count = len(my_dict[key])
            teacher = key   

    return teacher

【问题讨论】:

  • teacher = ""teacher 绑定到一个空字符串。当my_dict 为空时,这是一个很好的默认值(例如,循环永远不会迭代,也没有其他值被分配给teacher)。
  • 似乎只为max(my_dict, key=lambda x: len(my_dict[x])) 做了很多工作。
  • @TigerhawkT3:你的意思是max(my_dict, key=lambda x: len(my_dict[x]), default="")
  • default 在没有教师的情况下有效,但在字典中的教师没有班级的情况下无效。
  • @MartijnPieters,TigerhawkT3 你们处于另一个层次,我什至不明白你们发布的代码。 .大声笑。

标签: python variables python-3.x python-3.4


【解决方案1】:

它为教师分配默认值,它将被您代码中的实际教师名称替换。

【讨论】:

    【解决方案2】:

    teacher = "" 确保如果my_dict 为空,您将不会在不设置teacher = key 的情况下退出for 循环。否则,如果my_dict 为空,则teacher 将在未设置的情况下返回。

    如果你注释掉那一行,那么像这样调用你的函数:

    most_classes({})

    你会得到这个(因为teacher 在返回之前永远不会被初始化):

    UnboundLocalError: local variable 'teacher' referenced before assignment

    【讨论】:

      【解决方案3】:

      为什么不删除该行并测试它?

      def most_classes(my_dict):
          count = 0
          teacher = "" # this is where I am confused!
          for key in my_dict:
              if(len(my_dict[key]) > count):
                  count = len(my_dict[key])
                  teacher = key
      
          return teacher
      
      
      def most_classes_cavalier(my_dict):
          count = 0
          for key in my_dict:
              if(len(my_dict[key]) > count):
                  count = len(my_dict[key])
                  teacher = key
      
          return teacher
      
      
      if __name__ == "__main__":
          dic = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
                 'Kenneth Love': ['Python Basics', 'Python Collections']}
          print "The teacher with most classes is - " + most_classes(dic)
          print "The teacher with most classes is - " + most_classes_cavalier(dic)
          dic = {}
          print "The teacher with most classes is - " + most_classes(dic)
          print "The teacher with most classes is - " + most_classes_cavalier(dic)
      

      这是我运行程序时得到的 -

      The teacher with most classes is - Jason Seifer
      The teacher with most classes is - Jason Seifer
      The teacher with most classes is -
      Traceback (most recent call last):
        File "experiment.py", line 30, in <module>
          print "The teacher with most classes is - " + most_classes_cavalier(dic)
        File "experiment.py", line 20, in most_classes_cavalier
          return teacher
      UnboundLocalError: local variable 'teacher' referenced before assignment
      

      我看到@martijn-pieters 已经提供了解释,但在这种情况下,Python 解释器会更快地为您完成。

      【讨论】:

        猜你喜欢
        • 2012-06-15
        • 2022-10-05
        • 2018-03-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        相关资源
        最近更新 更多