在 NumPy 中逐元素计算一维数组的按位非
要逐元素计算一维数组的按位非,请在 Python NumPy 中使用 **numpy.bitwise_not()** 方法。计算输入数组中整数的底层二进制表示的按位非。此 ufunc 实现 C/Python 运算符 ~。
where 参数是广播到输入的条件。在条件为 True 的位置,out 数组将设置为 ufunc 结果。在其他位置,out 数组将保留其原始值。请注意,如果通过默认的 out=None 创建未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化状态。
步骤
首先,导入所需的库:
import numpy as np
创建一个一维数组:
arr = np.array([56, 87, 23, 92, 81, 98, 45, 98])
显示我们的数组:
print("Array...
",arr)获取数据类型:
print("
Array datatype...
",arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Elements in the Array...
",arr.size)要逐元素计算数组的按位非,请使用 numpy.bitwise_not() 方法:
print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))
示例
import numpy as np
#Create a 1d array
arr = np.array([56, 87, 23, 92, 81, 98, 45, 98])
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)
# To compute the bit-wise NOT of an array element-wise, use the numpy.bitwise_not() method in Python Numpy
print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))输出
Array... [56 87 23 92 81 98 45 98] Array datatype... int64 Array Dimensions... 1 Our Array Shape... (8,) Elements in the Array... 8 Result (bit-wise NOT)... [-57 -88 -24 -93 -82 -99 -46 -99]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP