0

Trying to make a list that puts the zeros onto the end. And it ignores the 0.0 which also need to be put on the end as a 0. Why is this happening?

Tried using float(0)/ 0.0. It works if I change it to a different integer just not 0.0.

Desired output [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def move_zeros(array):
    count = 0
    for x in array: #counts how many zeros
        if x is 0 or float(0):
            count+=1
    array = [x for x in array if x is not 0] # removes all zeros
    array = [x for x in array if x is not float(0)]
    for y in range(count):
        array.append(0) #tacks zero to the end of list

    print(array)

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

Expected to to work but it ignores 0.0

Alec
  • 8,529
  • 8
  • 37
  • 63

4 Answers4

1

is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

See this excellent answer for a more detailed explanation of the difference between is and ==.

As stated in other answers, you should use == and != in your case, since you are checking if values are equal, rather than if the two objects are the same objects in memory.

Here is your code, with the errors fixed:

def move_zeros(array):
    count = 0
    result = []
    for x in array: #counts how many zeros
        if x == 0 or x == float(0):
            count+=1

        elif x is not False and x is not None:
            result.append(x)

    for y in range(count):
        result.append(0) #tacks zero to the end of list

    print(result)

move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9])
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
  • There also may be False in the list sorry forgot to include that. That's why I used is rather than ==. But yes this answer was helpful. Thanks – stumpedstump Sep 26 '19 at 17:13
  • @stumpedstump added an update to account for that. Also I just realized that you really don't need the list comprehensions. Instead you should check the conditions in your intial `for x in array` loop. This way you only iterate once, instead of 3+ times. – Lord Elrond Sep 26 '19 at 17:24
0

You should not use is to perform arithmetic comparisons, and you can't combine two conditions using or like that. Change the condition as follows:

if x == 0:

Similarly fix conditions in your list comprehensions (x != 0 instead of x is not 0).

Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

There are a few problems:

  1. is evaluates if the two objects are the same object in memory. You want to use ==.
  2. if x is 0 or float(0) is not valid code. Use if x is 0 or x == float(0) or if x in (0, float(0) instead. You don't actually need to differentiate between 0 and float(0) though. Just use if x == 0 or if not x.
  3. This same problem is replicated in your list comprehensions. Use x != 0 or simply x instead of is not.
Alec
  • 8,529
  • 8
  • 37
  • 63
0

A note about or:

  • x == 0 or x == float(0) works
  • x in [0, float(0)] also works and is simpler
    • [x for x in array if x != 0] & [x for x in array if x != float(0)] can be replaced with [x for x in array if x not in [0, float(0)]]

Simplify the function

def move_zeros(array):
    zc = array.count(0)
    array = [x for x in array if x != 0] # removes all zeros
    array.extend([0 for _ in range(zc)])

    return array

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

y = move_zeros(test)

print(y)

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

Verify y against test:

from collections import Counter

print(test.count(0))
>>> 10

print(y.count(0))
>>> 10

print(len(test))
>>> 20

print(len(y))
>>> 20

test_dict = Counter(test)
print(test_dict)
>>> Counter({9: 4, 0.0: 10, 1: 4, 2: 1, 3: 1})

y_dict = Counter(test)
print(y_dict)
>>> Counter({9: 4, 0.0: 10, 1: 4, 2: 1, 3: 1})

Alternatively:

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

test_sorted = test.sort(reverse=True)
print(test_sorted)
>>> [9, 9, 9, 9, 3, 2, 1, 1, 1, 1, 0.0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158