Getting 'int' object is not iterable

后端 未结 3 686
慢半拍i
慢半拍i 2021-01-25 20:37
    cat_sums[cat] += value
TypeError: \'int\' object is not iterable

My input is this:

defaultdict(, {\'composed\'         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 21:23

    Each of your values is a list. The + operator, when applied to lists adds an iterable to a list. It doesn't append a single value:

    >>> [1,2] + [3,4]
    [1, 2, 3, 4]
    >>> [1,2] + 3
    TypeError: can only concatenate list (not "int") to list
    

    It looks like you want to do cat_sums[cat].append(value).

提交回复
热议问题