Getting attributes from arrays of objects in NumPy

后端 未结 2 1877
时光说笑
时光说笑 2020-12-09 20:26

Let\'s say I have an class called Star which has an attribute color. I can get color with star.color.

But what if I have a Num

2条回答
  •  攒了一身酷
    2020-12-09 21:26

    You could use numpy.fromiter(s.color for s in stars) (note lack of square brackets). That will avoid creating the intermediate list, which I imagine you might care about if you are using numpy.

    (Thanks to @SvenMarnach and @DSM for their corrections below).

提交回复
热议问题