【问题标题】:python array basic usage: step-function [closed]python数组基本用法:step-function [关闭]
【发布时间】:2016-04-17 12:57:55
【问题描述】:

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

谢谢!

【问题讨论】:

  • 你希望if t &gt; 3 做什么?
  • 顺便说一句,你应该考虑change your question's title
  • 是的,我确实想要一个阶梯函数!
  • @user3100115:仅供参考,您的标签添加已被另一个用户回滚 - 可以吗?它对我来说看起来不对,但我不知道这些技术。也许这只是一个数组问题,所以不需要它们?
  • @halfer 好吧,我添加了标签,因为这是一个 numpy|matplotlib 问题,所以不行。但是这个问题离题了,现在已经结束了,我当然不想通过编辑将它发送到重新打开投票队列。

标签: python arrays


【解决方案1】:
# 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

【讨论】:

  • 太棒了!谢谢,恐怕我有问题如何提问。还是向你学习,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多