Paul Chen - 10 months ago 50

Python Question

I'm a beginner of python, rather confused about the array or list usage. Please help me with a rather basic usage as below, I just want to separate the data into two section, but I don't know how to :

`# -*- coding: utf-8 -*`

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import math

from pylab import *

i = np.arange(2,5,0.1)

t = 1+i

Light = 10

if t > 3 :

Light = 5

plt.figure('God Bless: Lightcure')

plt.plot(i,Light)

plt.show()

But this didn't work, traceback as below:

`Traceback (most recent call last):`

File "1.py", line 11, in <module>

if t> 3 :

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Thank you !

Answer

```
# True is where condition is satisfied: numpy.ndarray([False, False, ..., True, True])
mask = t > 3
# Uninitialized array with same shape as t
Light = numpy.empty_like(t)
# Light elements set to 5 where corresponding mask elements are True
Light[mask] = 5
# Light elements set to 10 where corresponding mask elements are False
Light[~mask] = 10
```