python - Select Numpy Array of Objects by Objects Attributes -


i have numpy array of objects equity, i.e.:

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) 

the object equity has attribute exchange.

which concise method subarray containing equity objects exchange == 'new york stock exchange'?

thanks!

suppose numpy array named equity_array.

solution 1:

use list comprehension

np.array([eqt eqt in equity_array if eqt.exchange == 'new york stock exchange']) 

solution 2:

use python build-in function filter

np.array(filter(lambda x: x.exchange == 'new york stock exchange', equity_array)) 

notice in python 3 filter returns iterator should be

np.array(list(filter(lambda x: x.exchange == 'new york stock exchange', equity_array))) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -