[Tutor] anomaly
Peter Otten
__peter__ at web.de
Fri Apr 20 03:29:30 EDT 2018
Alan Gauld via Tutor wrote:
> On 20/04/18 05:53, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> I have a situation in which the same code gives an error in idle but
>> works in qtconsole
>> regards,
>>
>> *in idle*
>> v = np.zeros(len(x))
>>
>> for i in range(len(x)):
>> if x[i] < 1.0:
>> v[i] = 0
>
> You don't give us any clue to what x (or V) is,
> so its hard to know what is going on.
The code may be the same, but it's probably not fed the same data. I'm
guessing that x is a numpy array. The code above produces an error if the
array has more than one dimension:
$ python3
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> x = numpy.array([1,2,3])
>>> for i in range(len(x)):
... if x[i] < 1.0: pass
...
>>> x = numpy.array([[1,2,3], [4,5,6]])
>>> for i in range(len(x)):
... if x[i] < 1.0: pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
Comparing an array with a scalar uses broadcasting to build another array
>>> numpy.array([1,2,3]) < 3
array([ True, True, False], dtype=bool)
and the numpy developers forbid using an array in a boolean context because
it's not clear what bool(some_array) is supposed to return. Instead you have
to make explicit if you want all values or at least one value to be truthy.
> Also how are you running the code in IDLE?
> Are you running the same file using the Run
> menu option? Or are you importing your module?
>
> Also which Python version are you using?
More information about the Tutor
mailing list