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

前端 未结 2 1664
名媛妹妹
名媛妹妹 2020-12-03 08:27

My program looks like

# global
item_to_bucket_list_map = {}

def fill_item_bucket_map(items, buckets):
    global item_to_bucket_list_map

    for i in rang         


        
2条回答
  •  我在风中等你
    2020-12-03 09:15

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

    The while loop, however, can be simplified to

        for j in range(1, buckets / i + 1): # + 1 due to the <=
            if j == 1:
                mylist = []
            else:
                mylist = item_to_bucket_list_map.get(i)
            mylist.append(j)
            item_to_bucket_list_map[i] = mylist
    

提交回复
热议问题