从文本形式的记录列表创建recarray并在Numpy中基于索引获取数组
要从文本形式的记录列表创建recarray,请在Python Numpy中使用**numpy.core.records.fromrecords()**方法。名称使用“**names**”参数设置。字段名称,可以指定为逗号分隔的字符串,格式为'col1, col2, col3',也可以指定为字符串列表或元组,格式为['col1', 'col2', 'col3']。可以使用空列表,在这种情况下,使用默认字段名称('f0','f1',…)。数据类型使用“dtype”参数设置。
第一个参数是数据,同一字段中的数据可能是异构的——它们将被提升到最高数据类型。dtype是所有数组的有效dtype。formats、names、titles、aligned、byteorder参数,如果dtype为None,则这些参数将传递给numpy.format_parser以构造dtype。如果formats和dtype都为None,则将自动检测formats。为了更快地处理,请使用元组列表而不是列表的列表。
步骤
首先,导入所需的库:
import numpy as np
使用numpy.array()方法创建一个新数组:
arr1 = np.array([[7, 14, 21], [30, 37, 45]]) arr2 = np.array([[11.3, 18.7, 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)
要从文本形式的记录列表创建recarray,请在Python Numpy中使用numpy.core.records.fromrecords()方法:
rec = np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2, col3') print("
Record Array...
",rec)
获取数组值:
print("
Fetching the array1...
",rec[0]) print("
Fetching the array2...
",rec[1]) print("
Fetching the array3...
",rec[2])
示例
import numpy as np # Create a new array using the numpy.array() method arr1 = np.array([[7, 14, 21], [30, 37, 45]]) arr2 = np.array([[11.3, 18.7, 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 recarray from a list of records in text form, use the numpy.core.records.fromrecords() 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. # The datatype is set using the "dtype" parameter rec = np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2, col3') print("
Record Array...
",rec) print("
Fetching the array1...
",rec[0]) print("
Fetching the array2...
",rec[1]) print("
Fetching the array3...
",rec[2])
输出
Array1... [[ 7 14 21] [30 37 45]] Array2... [[11.3 18.7 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... [[('7', '14', '21') ('30', '37', '45')] [('11.3', '18.7', '24.0') ('87.5', '65.0', '23.8')] [('12', 'bbb', 'john') ('5.6', '29', 'k')]] Fhing the array1... [('7', '14', '21') ('30', '37', '45')] Fhing the array2... [('11.3', '18.7', '24.0') ('87.5', '65.0', '23.8')] Fhing the array3... [('12', 'bbb', 'john') ('5.6', '29', 'k')]
广告