我有一个Numpy对象数组 Equity ,即:
array([ Equity(24, symbol='AAPL', asset_name='APPLE INC', exchange='NASDAQ GLOBAL SELECT MARKET'),
Equity(26578, symbol='GOOG_L', asset_name='GOOGLE INC', exchange='NASDAQ GLOBAL SELECT MARKET'),
Equity(5061, symbol='MSFT', asset_name='MICROSOFT CORP', exchange='NASDAQ GLOBAL SELECT MARKET'),
...,
Equity(20513, symbol='LOOK', asset_name='LOOKSMART LTD', exchange='NASDAQ CAPITAL MARKET', ),
Equity(27133, symbol='WPCS', asset_name='WPCS INTERNATIONAL INC', exchange='NASDAQ CAPITAL MARKET'),
Equity(27917, symbol='FREE', asset_name='FREESEAS INC', exchange='NASDAQ CAPITAL MARKET')], dtype=object)
对象Equity具有交换属性。
获取仅包含Equity对象的子数组的最简洁方法是 exchange =='NEW YORK STOCK EXCHANGE'?
谢谢!
答案 0 :(得分:1)
假设numpy数组名为equity_array
。
使用列表理解
np.array([eqt for eqt in equity_array if eqt.exchange == 'NEW YORK STOCK EXCHANGE'])
使用Python内置函数filter
np.array(filter(lambda x: x.exchange == 'NEW YORK STOCK EXCHANGE', equity_array))
请注意,在Python 3中filter
会返回iterator
,因此它应该是
np.array(list(filter(lambda x: x.exchange == 'NEW YORK STOCK EXCHANGE', equity_array)))