Deep copy in python?


  • 0
    B

    It turns out i need deep copy in python for the code to work, because it is a list of list. What should I do?

    when I add from copy import deepcopy, i got compile error

    The code works fine in local machine:

    from copy import deepcopy
    
    def subsetsWithDup( S):
        if len(S)==0:
            return []
        S.sort()
        result=[[]]
        oldlist=[]
    
        for i in range(len(S)):
            if i>0 and S[i]==S[i-1]:
                temp=deepcopy(oldlist)
                oldlist=[]
            else:
                temp=deepcopy(result)
                oldlist=[]
            for j in temp:
                j.append(S[i])
                result.append(j)
                oldlist.append(j)
    
        return result
    

    print(subsetsWithDup([1,2,2]))


  • 0
    J

    Deep copy of list of lists of integers without reference cycles is easy to implement.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.