比较两个NumPy数组,如果一个数组大于另一个数组,则返回True


要比较两个数组,如果一个数组大于另一个数组则返回True,请在Python NumPy中使用**numpy.char.greater()**方法。

NumPy 提供了全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持各种硬件和计算平台,并且与分布式、GPU和稀疏数组库良好兼容。

步骤

首先,导入所需的库:

import numpy as np

创建两个一维字符串数组:

arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])

显示数组:

print("Array 1...
", arr1) print("
Array 2...
", arr2)

获取数组的类型:

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

获取数组的维度:

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

获取数组的形状:

print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)

要比较两个数组,如果一个数组大于另一个数组则返回True,请在Python NumPy中使用numpy.char.greater()方法。arr1和arr2是形状相同的两个输入字符串数组:

print("
Result...
",np.char.greater(arr1,arr2))

示例

import numpy as np

# Create two One-Dimensional arrays of string
arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To compare and return True if an array is greater than another array, use the numpy.char.greater() method in Python Numpy # The arr1 and arr2 are the two input string arrays of the same shape. print("
Result...
",np.char.greater(arr1,arr2))

输出

Array 1...
['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad' 'aaa']

Array 2...
['Cio' 'Tom' 'Cena' 'Kate' 'Adams' 'brad' 'aa']

Our Array 1 type...
<U5

Our Array 2 type...
<U5

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
1

Our Array 1 Shape...
(7,)

Our Array 2 Shape...
(7,)

Result...
[False False True False True False True]

更新于:2022年2月21日

476 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.