-1

Is there a way to ignore values in a list that create a division by zero in an iterative script instead of removing those problem values?

I'm thinking along the lines of if

if(x==0):
 break
elif(x!=0):
 continue

Where the values that aren't zero get to continue on through the script.

Matt
  • 3,508
  • 6
  • 38
  • 66
  • 2
    `continue` doesn't mean "keep going" it means "continue from the start of the loop" – John La Rooy Jul 04 '13 at 00:53
  • how do you know whether a elements is zero or not even before you know what it is? I mean, you should always check all the elements, no matter in a pre-treatment to the list or in a if-statement before division. – vvy Jul 04 '13 at 00:55
  • What if it's an array and there's a large enough number of them that you wouldn't be able to check very easily? – Matt Jul 04 '13 at 00:59
  • If two two below answers don't solve your problem, perhaps you should post some more code to further describe it. – RyPeck Jul 04 '13 at 01:06
  • Done. I wasn't descriptive enough. I'm gonna have to gather up some more info...it's not a question of single values, but arrays where the values within those may or may not be zero. – Matt Jul 04 '13 at 01:09
  • As a side note: It's usually better to just `try` something, and handle failure, than to check first. For example, instead of that `if x==0: break` then `else: y = 1/x`, do `try: y = 1/x` then `except ZeroDivisionError: break`. (Also, you shouldn't put parentheses around conditions in Python, and you should never try to use `elif [opposite of if condition]` when you just want `else`. – abarnert Jul 04 '13 at 01:20

5 Answers5

6

You can use list comprehension for a more efficient code,

from __future__ import division
num = [1,2,3,4,5,6,3,31,1,0,120,0,0]
divisor = 10
print [divisor/x for x in num if x != 0]

Output:

[10.0, 5.0, 3.3333333333333335, 2.5, 2.0, 1.6666666666666667, 3.3333333333333335, 0.3225806451612903, 10.0, 0.08333333333333333]
2

Of course. You can do what you did creating a if this, then that. Or you could even set up a try/except loop and catch the division by zero exception.

A trivial example -

>>> d = [1,0,3,4,5,6,0]
>>> for x in d:
...    if x == 0: 
...        continue  # skip this number since it will error.
...    print (5 / x)
... 
5
1
1
1
0
RyPeck
  • 7,830
  • 3
  • 38
  • 58
1

If you're doing a lot of arithmetic on arrays, you may want to consider using numpy. On top of being easier to use, and usually much faster, it's also more flexible.

For example:

>>> divisors = np.array([1,2,3,4,5,6,3,31,1,0,120,0,0])
>>> fractions = 10. / divisors
>>> fractions
array([ 10.        ,   5.        ,   3.33333333,   2.5       ,
         2.        ,   1.66666667,   3.33333333,   0.32258065,
        10.        ,          inf,   0.08333333,          inf,          inf])

Compare to:

>>> fractions = []
>>> for divisor in divisors:
...     if divisor == 0:
...         fractions.append(float('inf'))
...     else:
...         fractions.append(10. / divisor)

Or even:

>>> fractions = [10. / divisor if divisor else float('inf') 
...              for divisor in divisors]

numpy isn't always the answer, but it's worth taking a look at.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

The best way should be to use exception handling to just show an error message.

    try:
        foo_with_possible_division_by_zero()
    except ZeroDivisionError:
        print "Warning: NaN encountered!"

If you do not want a message just replace the print statement with pass

jadelord
  • 1,511
  • 14
  • 19
0

The easiest way to ignore zero divisions I think can be done arithmetically by simply adding a small decimal to the divisor.

eg. if g = 0 and you want to compute g/g. you can get around that this way:

g/(g+0.0000001) The best thing about this is you will get your computation to behave algebraically similar to 0/0 = 0