【问题标题】:Appending in python在python中追加
【发布时间】:2017-08-13 19:51:09
【问题描述】:

在 python 问题中追加:

   #other code irrelevant to q
        if #code:
            key3_lst = key3_lst.append(b.month)
            b_w_dic[key3] = key3_lst
        if  #code:
            key4_lst = key4_lst.append(b.month)
            b_w_dic[key4] = key4_lst
    return b_w_dic

我收到一条错误消息,说在 key3_lst = key3_lst.append(b.month) builtins.AttributeError: 'NoneType' object has no attribute 'append'

【问题讨论】:

    标签: python list python-3.x class dictionary


    【解决方案1】:

    您不会附加并将其设置为变量。而不是 key3_lst = key3_lst.append(b.month) 这将使 key3_lst = None 只是追加而不给它一个变量 key3_lst.append(b.month)

    【讨论】:

      【解决方案2】:

      append 方法应用更改并返回 None。

      >>> data = [0, 1, 2 ,3]
      >>> data
      [0, 1, 2, 3]
      >>> data.append(4)
      >>> data
      [0, 1, 2, 3, 4]
      >>> data = data.append(5)
      >>> type(data)
      <class 'NoneType'>
      

      【讨论】:

        【解决方案3】:
        key3_lst = key3_lst.append(b.month)
        

        “append”函数就是一个函数,返回类型为“None”

        只需使用:

        key3_lst.append(b.month)
        

        【讨论】:

        • 谢谢!我会更新这个问题,只写一行,因为其余的都无关紧要
        猜你喜欢
        • 1970-01-01
        • 2014-05-09
        • 2014-05-08
        • 1970-01-01
        • 2013-10-19
        • 2017-08-24
        • 1970-01-01
        相关资源
        最近更新 更多