0 votes

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?

asked Apr 9 in Projects by praveenyadav1602 (17 points)
retagged Apr 9 by praveenyadav1602

2 Answers

0 votes

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])
answered Apr 9 by supper_raptor (715 points)

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)

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

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)

@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

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)
+1 vote

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.

answered 6 days ago by Poobslag (68 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.