Setting to zero of elements of a numpy array different from a value (python3) : a[a <> 5] = 0
<p>In python 2, the following code works:</p>

<pre><code>a = np.array([[1,5],[2,3]])
print a
print()
a[a&lt;2] = 0
print a
a[a &lt;&gt; 5] = 0
print a
</code></pre>

<p>But in python3, it triggers a syntax error:</p>

<pre><code>a[a &lt;&gt; 5] = 0

File "&lt;ipython-input-14-165e29d9f8e4&gt;", line 1
    a[a &lt;&gt; 5] = 0
         ^
SyntaxError: invalid syntax
</code></pre>

<p>In Python 3, <code>&lt;&gt;</code> was replaced by <code>!=</code>. It is similar to how <code>print</code> was changed from a statement to a function. See <a href="https://docs.python.org/2/library/stdtypes.html#comparisons" rel="nofollow">Comparisons</a> in the Docs:</p>

<blockquote>
  <p><code>!=</code> can also be written <code>&lt;&gt;</code>, but this is an obsolete usage kept for backwards compatibility only. New code should always use <code>!=</code>.</p>
</blockquote>

<p>P.s: You can be quite sneaky and do:</p>

<pre><code>from __future__ import barry_as_FLUFL
</code></pre>

<p>which allows <code>&lt;&gt;</code> and makes <code>!=</code> a SyntaxError, but really don't, just use <code>!=</code>. </p>

python arrays python-3.x numpy