-1

Here's the deal, i have an array of multiples elements with about half of it being zeros. I want to remove these zeros by using a function instead of the traditional x=x[x!=0].

I tried:

def funct(x,y):
    x=x[x!=0]
    y=y[y!=0]

But the output i get is the same variable i had before i execute the function. An array with multiple zeros.

I'm new with python so sorry if this question sound ridiculous.

Thank you very much!

4

1 回答 1

5

x[x!=0]返回一个新数组,并将该新数组分配给局部变量x

你可以做:

def funct(x, y):
    x = x[x!=0]
    y = y[y!=0]
    # do something here
    return x,y
a, b = funct(a, b)  #assign the returned value back to the global variables
于 2013-08-13T18:35:11.670 回答