python if 语句绝对值_在if语句条件中使用数组

如何在if语句的条件部分使用数组?我希望程序检查每个元素的绝对值,并返回相应的部分。这给了一些希望:Function of Numpy Array with if-statement

但这种技术在我的情况下是行不通的。

这是我正在尝试的代码:def v(x,y):

if x[abs(x)

return #something 1

if x[a

return #something 2

if x[b

return #something 3

这里,x和y是数组。(实际上是由x,y = ogrid[-R:R:.01, -R:R:.01]创建的网格)

编辑:我发现的最好的方法(经过多次尝试和错误)是使用布尔数组。代码如下:#Create a grid of coordinates. XY-Plane.

x,y=ogrid[-R:R+1:1,-R:R+1:1]

#Create the polar distance 2D array. Conditions are tested on this array.

r=sqrt(x**2+y**2)

#Create 2D array to hold the result

v=zeros([len(x*y),len(x*y)],float)

#idr is the boolean 2D array of same size and shape as r.

#It holds truth values wherever the condition is true.

idr=(r

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值