Python:AttributeError:'NoneType'对象没有属性'append' [英] Python : AttributeError: 'NoneType' object has no attribute 'append'

查看:230
本文介绍了Python:AttributeError:'NoneType'对象没有属性'append'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序看起来像

# global
item_to_bucket_list_map = {}

def fill_item_bucket_map(items, buckets):
    global item_to_bucket_list_map

    for i in range(1, items + 1):
        j = 1
        while i * j <= buckets:
            if j == 1:
                item_to_bucket_list_map[i] = [j]
            else:
                item_to_bucket_list_map[i] = (item_to_bucket_list_map.get(i)).append(j)
            j += 1
        print "Item=%s, bucket=%s" % (i, item_to_bucket_list_map.get(i))


if __name__ == "__main__":
    buckets = 100
    items = 100
    fill_item_bucket_map(items, buckets)

当我运行它时,它会把我扔掉

When I run this, it throws me

AttributeError: 'NoneType' object has no attribute 'append'

不确定为什么会这样吗?当我已经在每个j

Not sure why this would happen? When I am already creating a list at start of each j

推荐答案

实际上,您将None存储在此处: append()更改列表并返回None

Actually you stored None here: append() changes the list in place and returns None

 item_to_bucket_list_map[i] = (item_to_bucket_list_map.get(i)).append(j)

示例:

In [42]: lis = [1,2,3]

In [43]: print lis.append(4)
None

In [44]: lis
Out[44]: [1, 2, 3, 4]

这篇关于Python:AttributeError:'NoneType'对象没有属性'append'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆