从数组(扁平)列表创建记录数组并在 Numpy 中使用名称获取数组
要从数组(扁平)列表创建记录数组,请在 Python Numpy 中使用 **numpy.core.records.fromarrays()** 方法。名称使用 **"names"** 参数设置。字段名称,可以以 'col1, col2, col3' 形式的逗号分隔字符串的形式指定,也可以以 ['col1', 'col2', 'col3'] 形式的字符串列表或元组的形式指定。可以使用空列表,在这种情况下使用默认字段名称('f0','f1',…)。
它返回由给定 arrayList 列组成的记录数组。第一个参数是数组类对象(如列表、元组和 ndarray)的列表。dtype 是所有数组的有效 dtype。格式、名称、标题、对齐、字节序参数,如果 dtype 为 None,则将这些参数传递给 numpy.format_parser 以构造 dtype。
步骤
首先,导入所需的库 -
import numpy as np
使用 numpy.array() 方法创建一个新数组 -
arr1 = np.array([[5, 10, 15], [20, 25, 30]]) arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]]) arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])
显示数组 -
print("Array1...
",arr1)
print("Array2...
",arr2)
print("Array3...
",arr3)获取数组的类型 -
print("
Array1 type...
", arr1.dtype)
print("
Array2 type...
", arr2.dtype)
print("
Array3 type...
", arr3.dtype)获取数组的维度 -
print("
Array1 Dimensions...
", arr1.ndim)
print("
Array2 Dimensions...
", arr2.ndim)
print("
Array3 Dimensions...
", arr3.ndim)要从数组(扁平)列表创建记录数组,请在 Python Numpy 中使用 numpy.core.records.fromarrays() 方法。名称使用 "names" 参数设置 -
rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'i,j,k')
print("
Record Array...
",rec)根据名称获取数组 -
print("
Fetching the array1...
",rec.i)
print("
Fetching the array2...
",rec.j)
print("
Fetching the array3...
",rec.k)示例
import numpy as np
# Create a new array using the numpy.array() method
arr1 = np.array([[5, 10, 15], [20, 25, 30]])
arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]])
arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])
# Display the arrays
print("Array1...
",arr1)
print("Array2...
",arr2)
print("Array3...
",arr3)
# Get the type of the arrays
print("
Array1 type...
", arr1.dtype)
print("
Array2 type...
", arr2.dtype)
print("
Array3 type...
", arr3.dtype)
# Get the dimensions of the Arrays
print("
Array1 Dimensions...
", arr1.ndim)
print("
Array2 Dimensions...
", arr2.ndim)
print("
Array3 Dimensions...
", arr3.ndim)
# To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method in Python Numpy
# The names is set using the "names" parameter
# The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3'].
# An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used.
rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'i,j,k')
print("
Record Array...
",rec)
# Fetching the arrays based on names
print("
Fetching the array1...
",rec.i)
print("
Fetching the array2...
",rec.j)
print("
Fetching the array3...
",rec.k)输出
Array1... [[ 5 10 15] [20 25 30]] Array2... [[ 9. 18. 24. ] [87.5 65. 23.8]] Array3... [['12' 'bbb' 'john'] ['5.6' '29' 'k']] Array1 type... int64 Array2 type... float64 Array3 type... <U4 Array1 Dimensions... 2 Array2 Dimensions... 2 Array3 Dimensions... 2 Record Array... [[( 5, 9. , '12') (10, 18. , 'bbb') (15, 24. , 'john')] [(20, 87.5, '5.6') (25, 65. , '29') (30, 23.8, 'k')]] Fhing the array1... [[ 5 10 15] [20 25 30]] Fhing the array2... [[ 9. 18. 24. ] [87.5 65. 23.8]] Fhing the array3... [['12' 'bbb' 'john'] ['5.6' '29' 'k']]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP