python数组基本用法:step-function

时间:2016-04-17 12:57:56

标签: python arrays

我是python的初学者,对数组或列表用法感到困惑。请帮我一个相当基本的用法如下,我只是想将数据分成两部分,但我不知道如何:

# -*- 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()

但这不起作用,追溯如下:

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()

谢谢!

1 个答案:

答案 0 :(得分:0)

# 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