Python TypeError: descriptor ‘append’ for ‘list’ objects doesn’t apply to a ‘int’ object

pythonpython-3.x

I am trying to solve a LeetCode problem in python. Given a list of integers and a target, we must find all the unique combination of integers in the list whose sum is equal to the target. The list can have duplicate integers but the combinations of the integers (whose sum is equal to the target) must be unique in the result. The list will have only positive integers https://leetcode.com/problems/combination-sum-ii/

Below is the code:

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = List[List[int]]
        c = List[int]
        self.combSum2(0,sorted(candidates),target,result,c)
        return result

    def combSum2(self, i: int, l: List[int], t: int, res: List[List[int]], curr: List[int]):
        if t == 0:
            print(curr)
            res.append(curr)
            return
        if t < 0:
            return
        for idx in range(i,len(l):
            if(idx == i or l[idx] != l[idx-1]):
                curr.append(l[idx])
                self.combSum2(idx+1,l,t-l[idx],res,curr)
                del curr[-1] 

The code does produce unique combinations, however, when I run it I am getting this error:
TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'int' object at the line curr.append(l[idx]). enter image description here

How to resolve this? Any help would be appreciated.

EDIT:

I have tried what @user2357112 supports Monica suggested and changed my code:

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        c = []
        self.combSum2(0,sorted(candidates),target,result,c)
        print("result:")
        print(result)
        return result

    def combSum2(self, i: int, l: List[int], t: int, res: [], curr: []):
        if t == 0:
            print(curr)
            res.append(curr)
            return
        if t < 0:
            return
        for idx in range(i,len(l)):
            if(idx == i or l[idx] != l[idx-1]):
                curr.append(l[idx])
                self.combSum2(idx+1,l,t-l[idx],res,curr)
                del curr[-1]

Now the error is gone but the result is empty:

[1, 1, 6]
[1, 2, 5]
[1, 7]
[2, 6]
result:
[[], [], [], []]

The combinations are being created, but not getting appended to the result.

I can't figure out where the bug is. Any help would be appreciated. Thank you.

Best Answer

You might be using a deque, once I also encountered with the same problem, the error is because: you are appending to the deque directly (if you are using it) without initializing a list.

error prone code:

from collections import defaultdict, deque
d = defaultdict(lambda: deque)
a = [1, 2, 3]
for i in range(3):
   d[a[i]].append(i)

Error free code:

from collections import defaultdict, deque
d = defaultdict(lambda: deque([])

look in previous error says it doesn't apply to int object, actually it doesn't apply to anything, until you declared a list in the deque.

so overall, always declared like deque([]).

Related Topic