【发布时间】:2020-11-05 13:52:43
【问题描述】:
我想获取包含在 ndarray 中的每个对象的属性。考虑下面的代码,在向量化之后,函数返回一个带有对象的 ndarray。从数组中的每个对象中,我想获取属性并将属性存储在一个新数组中。
类和函数是更复杂的类和函数的虚拟对象。我知道从stud = new_student(["Michael","Rachel"], [1,2]); stud.name 可以得到一个包含名称的列表,但是这对于我使用的函数而不是new_student 是不可能的。
import numpy as np
class Student:
# class variable
stream = "COE"
# Constructor
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
def new_student(name, roll_no):
return Student(name, roll_no)
new_student_vec = np.vectorize(new_student)
studArr = new_student_vec(["Michael","Rachel"], [1,2])
studArr.name
【问题讨论】:
-
为什么不直接使用这些实例的列表?