I need to slice an array from a given index until a certain condition is met.
>>> a = numpy.zeros((10), dtype='|S1')
>>> a[2] = 'A'
>>> a[4] = 'X'
>>> a[8] = 'B'
>>> a
array(['', '', 'A', '', 'X', '', '', '', 'B', ''], dtype='|S1')
For instance, for the above array I want a subset from a given index until first non-zero values in both directions. For example, for index values 2, 4, 8 the results would be:
['', '', A, ''] # 2
['', X, '', '', ''] # 4
['', '', '', B, ''] # 8
Any suggestions on the simplest way to do this using the numpy API? Learning python and numpy, would appreciate any help. Thanks!
objectarrays (not very common and not very memory-efficient) presents a particular problem when trying to determine the index of non-None array items. Could you be persuaded to use a fixed-byte dtype? If you are committed to theobjectdtype, then is it true that anything "non-None" will evaluate toTruewhen typecast as abool? Either of those would help simplify things a lot.objectarray to store single character strings. Essentially, all I need is achararray. Is there an alternativedtypeI could usedtype?dtype='|S1'(or simplydtype=str) for single-character strings.dtype='S1'