Home > Blockchain >  How to make 2 deep copy of a list?
How to make 2 deep copy of a list?

Time:10-26

I am making 2 deep copies of a list. The problem is that first deep copy behaves fine, but second deep copy of the list is being altered based on changes on my original list.

import copy

def findThreeLargestNumbers(array):
    # Write your code here.
    org_array2=copy.deepcopy(array)

    org_array1=copy.deepcopy(array)
    
    largest1=0
    largest2=0
    largest3=0
    for i in range(0,len(array)-1):
        if(array[i] >= array[i 1]):
            array[i 1]=array[i]
    largest1=array[-1]
    print(largest1)
    print(array)
    org_array1.remove(largest1)
    
    for i in range(0,len(org_array1)-1):
        if(org_array1[i] >= org_array1[i 1]):
            org_array1[i 1]=org_array1[i]
    largest2=org_array1[-1]
    print(largest2)
    print(org_array1)
    org_array2.remove(largest2)
    
    for i in range(0,len(org_array2)-1):
        if(org_array2[i] >= org_array2[i 1]):
            org_array2[i 1]=org_array2[i]
    largest3=org_array2[-1]
    print(largest3)
    print(org_array2)
array= [10,5,9,10,12,3,517,69,81,74]
findThreeLargestNumbers(array)

517
[10, 10, 10, 10, 12, 12, 517, 517, 517, 517]
81
[10, 10, 10, 10, 12, 12, 69, 81, 81]
517
[10, 10, 10, 10, 12, 12, 517, 517, 517]

I want my both the newly copied lists to be independent of each other, as well as independent of original list.

CodePudding user response:

Your copies are actually fine. The issue is that you have only removed the second largest value from org_array2. So when your logic looks for the largest value, it finds 514 again. Here's your code with that logic corrected:

import copy

def findThreeLargestNumbers(array):
    # Write your code here.
    org_array2=copy.deepcopy(array)

    org_array1=copy.deepcopy(array)
    
    largest1=0
    largest2=0
    largest3=0
    for i in range(0,len(array)-1):
        if(array[i] >= array[i 1]):
            array[i 1]=array[i]
    largest1=array[-1]
    print(largest1)
    print(array)
    org_array1.remove(largest1)
    
    for i in range(0,len(org_array1)-1):
        if(org_array1[i] >= org_array1[i 1]):
            org_array1[i 1]=org_array1[i]
    largest2=org_array1[-1]
    print(largest2)
    print(org_array1)
    org_array2.remove(largest2) # you are removing the 2nd largest only - leaving the largest
    org_array2.remove(largest1) # here you need to remove the largest one as well
    
    for i in range(0,len(org_array2)-1):
        if(org_array2[i] >= org_array2[i 1]):
            org_array2[i 1]=org_array2[i]
    largest3=org_array2[-1]
    print(largest3)
    print(org_array2)
array= [10,5,9,10,12,3,517,69,81,74]
findThreeLargestNumbers(array)

Here are the results:

$ python findlargest
517
[10, 10, 10, 10, 12, 12, 517, 517, 517, 517]
81
[10, 10, 10, 10, 12, 12, 69, 81, 81]
74
[10, 10, 10, 10, 12, 12, 69, 74]

CodePudding user response:

If the goal is to find the largest n values (in this case largest three values), consider sorting list in reverse order and then, extracting the first n non repeating values from the sorted list.

from copy import copy, deepcopy
from typing import List

array= [10,5,9,10,12,3,517,69,81,74]

def find_nth_largest(array: 'List', n: int) -> 'List':
    # copy1 = deepcopy(array)
    # Here deepcopy is not necessary since you don't have nested lists shallow copy will  do. 
    copy1 = array[:] # this gets you a copy of one dimensional array. 
    copy1.sort(reverse=True)
    largest_n, count = [copy1[0]], 0
    for item in copy1[1:]:
        if count < n - 1:
            if item != largest_n[-1]:
                largest_n.append(item)
                count  = 1
        else:
            return largest_n
    return largest_n

print(find_nth_largest(array, 3))

CodePudding user response:

I wanted to play with the heapq.largest method suggested by ShadowRanger and I came up with this bit of code (doesn't answer the OC, but it's good to know):

import heapq
array= [10,5,517,9,10,81,12,3,517,69,81,74]

# Repeats are not ok
print( sorted(list(set(array)))[-3:] )

# repeats allowed
print( sorted(array)[-3:] )

# repeats not allowed
print(heapq.nlargest( 3, list(set(array)) ) )

# repeats allowed
print(heapq.nlargest( 3, array ) )

Here are the results:

[74, 81, 517]
[81, 517, 517]
[517, 81, 74]
[517, 517, 81]

Fortunately, the results are the same (more or less). I had to modify the input a little bit to give extra large values.

The list(set(array)) was used to eliminate "repeats".

  • Related