-2

I have this question where I need to put every 0 or 0.0 at the back of the list while saving the order of the other elements, for example:

move_zero([1,3,4,[],False,None,0,0,3,0.0,4])

Will output:

[1,3,4,[],False,None,3,4,0,0,0]

Note that the function turned especially 0.0 to 0 and it's fine, this is what it should do

Now, this is my go at this question:

def move_zeros(array):
   new_arr = []
   count_zero = 0
   for x in array:
       if (x is 0) or (x is 0.0):
           count_zero += 1
       else:
           new_arr.append(x)

   return new_arr + count_zero * [0]

But for some reason which I cannot call why, it does not enter the first if statement where my input is:

[9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9]

The output is:

[9, 0.0, 9, 1, 2, 1, 1, 0.0, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0]

It outputs the wrong list on Python 3.6 While on python 3.8.2 it works fine.. (However I need to use 3.6 for this)
Where am I wrong? the if statement seems ok!

Thank you!

CodeCop
  • 1
  • 2
  • 15
  • 37
  • 4
    you want `==`, `is` is for comparing identity -- if you run your code with `-Wonce` it will produce a `SyntaxWarning` – anthony sottile Apr 12 '20 at 07:06
  • @AnthonySottile If I won't use the is statement, then it will enter it if we get to an element 'False' in the list which we don't want, because 0 == False – CodeCop Apr 12 '20 at 07:07
  • 1
    then use `x == 0 and x is not False` -- note that comparing with singletons like `True` / `False` / `None` *should* use `is` / `is not` (identity comparison) – anthony sottile Apr 12 '20 at 07:08
  • https://stackoverflow.com/questions/1504717/why-does-comparing-strings-using-either-or-is-sometimes-produce-a-differe?rq=1 is a likely duplicate -- it's not an exact one and I'm gold badge so I don't really want to use my hammer on this one :) – anthony sottile Apr 12 '20 at 07:09
  • Even better than `x is not False` is just using the truthiness of `x` in most cases, e.g. `if x:`. – ggorlen Apr 12 '20 at 07:09
  • @AnthonySottile why would you do it :( I did not ask why its different, I asked how I can solve this question :( because my go at it went wrong2 – CodeCop Apr 12 '20 at 07:11
  • @OUR I didn't do anything other than comment here -- if I did use my CV it would be closed – anthony sottile Apr 12 '20 at 07:13
  • @OUR You are appending an array of `0`s rather than the correct sequence of `0`s and `0.0` that your original array had. If you debugged your code, you would see that. – ivan_pozdeev Apr 12 '20 at 07:17
  • @ivan_pozdeev no I don't, im concatenating both arrays, [1,2,3] + [0]*3 = [1,2,3,0,0,0] – CodeCop Apr 12 '20 at 07:18
  • `[0]*3` = `[0,0,0]` (all `0`s). Your question sounds like some of those should be `0.0`s instead. – ivan_pozdeev Apr 12 '20 at 07:21
  • _"I need to put every 0 or 0.0 at the back of the list while saving the order of the other elements"_ -- is says "move" rather than "move and convert to 0s". The expression _"Will output:"_ is unclear whether this is the right or the actual output. So the question is unclear and is worth a downvote at least as such. – ivan_pozdeev Apr 12 '20 at 07:27
  • https://stackoverflow.com/questions/15008380/double-equals-vs-is-in-python – ivan_pozdeev Apr 12 '20 at 07:33
  • The author of the answer also misunderstood your question AFAICS. – ivan_pozdeev Apr 12 '20 at 07:37

2 Answers2

1

But for some reason which I cannot call why, it does not enter the first if statement where my input is:

You need to use == for comparing values. is is used to compare identity in Python. See this for information on == vs is in Python.

Try this:

def move_zeros(a):
    r = [i for i in a if not isinstance(i, (float, int)) or str(i) not in ('0.0', '0')]
    r += [0] * (len(a) - len(r))
    return r

print(move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9]))

Outputs:

[9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
abhiarora
  • 9,743
  • 5
  • 32
  • 57
-1

Try this

def move_zeros(a):
    res = []
    x = 0
    for num in a:
        if str(num) == '0.0' or str(num) == '0':
            x += 1
        else:
            res.append(num)

    return res + [0] * x

print(move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9]))

Output:

[9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
deadshot
  • 8,881
  • 4
  • 20
  • 39