如何解决我想要一个只能使用布尔掩码将值除以3或5的新数组
下面的代码给出了一个错误,我不知道如何通过其他方式获得所需的输出。
public Bitmap getPictureBitmap(String yourPathToPicture) throws IOException {
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
ExifInterface ei = new ExifInterface(yourPathToPicture);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap,90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap,180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap,270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
}
return rotatedBitmap;
}
public static Bitmap rotateImage(Bitmap source,float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source,source.getWidth(),source.getHeight(),matrix,true);
}
输出- ValueError:具有多个元素的数组的真值不明确。在第4行使用a.any()或a.all()
解决方法
为or
使用内置的numpy函数:
mask=np.logical_or(arr%3==0,arr%5==0)
arr[mask]
#[ 0 3 5 6 9 10 12 15 18 20 21 24]
,
您的代码中有两件事需要更改:
- 您需要用&/ | 替换和/或
- 您必须为条件添加括号,以确保评估中没有歧义。
下面是更新的代码:
import numpy as np
arr=np.arange(25).reshape(5,5)
print(arr[(arr%3==0) | (arr%5==0)])
# Output: [ 0 3 5 6 9 10 12 15 18 20 21 24]
,
arr%3==0 or arr%5==0
对两个布尔变量进行“或”运算将给出布尔答案,因此不能将其用作索引。要获取所有值,下面是一个简单易读的代码:
ans = []
for array in arr:
for i in array:
if i%3==0 or i%5==0:
ans.append(i)
print(ans)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。