比较两个 NumPy 数组,如果第一个数组小于等于第二个数组,则返回 True
要比较两个数组,如果一个数组小于等于另一个数组,则返回 True,请在 Python NumPy 中使用 **numpy.char.less_equal()** 方法。arr1 和 arr2 是两个具有相同形状的输入字符串数组。
与 numpy.less_equal 不同,此比较是通过首先从字符串末尾去除空格字符来执行的。此行为是为了与 numarray 保持向后兼容。
numpy.char 模块为类型为 numpy.str_ 或 numpy.bytes_ 的数组提供了一组向量化字符串操作。
步骤
首先,导入所需的库 -
import numpy as np
创建两个字符串的一维数组 -
arr1 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa']) arr2 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
显示数组 -
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,请使用 numpy.char.less_equal() 方法。arr1 和 arr2 是两个具有相同形状的输入字符串数组 -
print("
Result...
",np.char.less_equal(arr1,arr2))
示例
import numpy as np
# Create two One-Dimensional arrays of string
arr1 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])
arr2 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
# 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 less than equal to another, use the numpy.char.less_equal() method in Python Numpy
# The arr1 and arr2 are the two input string arrays of the same shape.
print("
Result...
",np.char.less_equal(arr1,arr2))输出
Array 1... ['Cio' 'Tom' 'Cena' 'Kate' 'Adams' 'brad' 'aa'] Array 2... ['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad' 'aaa'] 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 True True True True False True]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP