subtract list from list

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By praveenyadav1602

how to subtract list form list

var x = [1,2,3,4,5,6,7,8,9,0]  

var y = [1,3,5,7,9]  
  
var r =  (y - x)   # (should return [2,4,6,8,0])

also tried:

x = list(set(x)-set(y))

how to get desire output?

:bust_in_silhouette: Reply From: supper_raptor

You can make a function for this.

func subtractArr(a : Array, b : Array) -> Array:
	var final_arr = Array()
	a.sort()
	b.sort()
	var b_index = 0
	var b_size = b.size()
	
	for i in a:
		if b_index >= b_size or i != b[b_index]:
			final_arr.append(i)
		if i >= b[b_index]:
			b_index += 1
	
	return final_arr

Usage example

var x = [1,2,3,4,5,6,7,8,9,0]  

var y = [1,3,5,7,9] 
var r = subtractArr(x,y)   # (will return [2,4,6,8,0])

and how to print r
i tried:

func subtractArr(a : Array, b : Array) -> Array:
    var final_arr = Array()
    a.sort()
    b.sort()
    var b_index = 0
    var b_size = b.size()

    for i in a:
        if b_index >= b_size or i != b[b_index]:
            final_arr.append(i)
        if i >= b[b_index]:
            b_index += 1

    return final_arr

var x = [1,2,3,4,5,6,7,8,9,0]  

var y = [1,3,5,7,9] 
var r = subtractArr(x,y)
func prin():
	print (r)

praveenyadav1602 | 2020-04-09 07:17

Why not just

print(r)

Instead of declaring a whole function for this? However, if you do so, you also have to call your declared function to execute it:

func prin():
    print(r)
prin() # Call the function to execute it

Blackthorn | 2020-04-09 07:34

You can make a function to print and final code will be -

func printArr(arr : Array):
	for i in arr:
		print(i)

func subtractArr(a : Array, b : Array) -> Array:
    var final_arr = Array()
    a.sort()
    b.sort()
    var b_index = 0
    var b_size = b.size()

    for i in a:
        if b_index >= b_size or i != b[b_index]:
            final_arr.append(i)
        if i >= b[b_index]:
            b_index += 1

    return final_arr

var x = [1,2,3,4,5,6,7,8,9,0]  

var y = [1,3,5,7,9] 
var r = subtractArr(x,y)
printArr(r)
   

supper_raptor | 2020-04-09 07:36

@blackthorn

var y = [1,3,5,7,9] 
var r = subtractArr(x,y)
print (r)

not working returened error unexpected token: Build-in Func:

func prin():
    print(r)
prin()

above also not working error:unexpected token: Identifier:prin

@supper_raptor whole code snipt copy paste but not working error:unexpected token: Identifier:prin

checked indent level, but not working

praveenyadav1602 | 2020-04-09 08:01

Bro printArr and subtractArr are functions

printArr prints array and substractArray substracts array/list

You need to call substractArray then printArr

func printArr(arr : Array):
    for i in arr:
        print(i)

func subtractArr(a : Array, b : Array) -> Array:
    var final_arr = Array()
    a.sort()
    b.sort()
    var b_index = 0
    var b_size = b.size()

    for i in a:
        if b_index >= b_size or i != b[b_index]:
            final_arr.append(i)
        if i >= b[b_index]:
            b_index += 1

    return final_arr

Both are functions you need to call them to use.
for example if you need to use them in ready() ,then the code is

func _ready():
	var x = [1,2,3,4,5,6,7,8,9,0]  

	var y = [1,3,5,7,9] 
	var r = subtractArr(x,y)   # (will return [2,4,6,8,0])
	printArr(r)

supper_raptor | 2020-04-09 10:40

:bust_in_silhouette: Reply From: Poobslag

Here’s an alternative subtract function, based on Apache Commons Collections:

static func subtract(a: Array, b: Array) -> Array:
    var result := []
    var bag := {}
    for item in b:
        if not bag.has(item):
            bag[item] = 0
        bag[item] += 1
    for item in a:
        if bag.has(item):
            bag[item] -= 1
            if bag[item] == 0:
                bag.erase(item)
        else:
            result.append(item)
    return result

supper_raptor’s solution fails for some edge cases, such as subtract([1, 1, 1, 1, 1], [1, 1, 1]) which actually throws an error. This implementation returns the correct result.

There is even simpler solution for this:

func subtract_arrays(a: Array, b: Array) -> Array:
	var result = a.duplicate()
	
	for item in b:
		result.erase(item)
	
	return result

Function erase removes the first occurrence of a value from the array. If the value does not exist in the array, nothing happens. That means you don’t have to check if the value is in the array.

You have to duplicate the a array, otherwise it will get modified.