Does arcpy.da.SearchCursor store the results in an array?
As described in SearchCursor-Help, SearchCursor
returns iterator. It means that you cannot use cursor[0][1]
. In any case, you have to use for loop to iterate over features.
But using list comprehension may help you a little:
from arcpy.da import SearchCursor
var = 2
# features with c1=2
whereClause = '[c1]' + '=' '\'' + str(var) +'\''
# get all (c1,c2) pairs, if c2!=1
# list comprehension
cursor = [ x for x in SearchCursor("table", ['c1','c2'], whereClause) if x[1]!=1 ]
# then, cursor list have all '(c1,c2) value pairs' of features with c2!=1
# in your case, all cursor[i][0] equals 2 because whereClause equals 'c1=2'
# all cursor[i][1] doesn't equal 1.
# for example: cursor = [(2, 0), (2, 10), (2, 7), (2, 200), ...]
If you want to work with feature attributes as an array and do away with the computationally expensive for
loop, you can use TableToNumPyArray()
to convert the attributes to a numpy array. This unleashes the full numpy arsenal. For example:
import arcpy
import numpy as np
fc = r'C:\path\to\your\geodb.gdb\featureclass'
array = arcpy.da.TableToNumPyArray(fc, "*")
Let's look at the structure of the numpy array:
>>> array
array([ (1, [-90.76623071185851, 48.05919100478101], 1.6963634949176454, 0.22899603992269765, 1, -90.76623071185851, 48.05919100478101, u'12:01'),
(2, [-92.53108497604227, 47.732022361872], 1.7467078562344307, 0.2427899374179541, 2, -92.53108497604227, 47.732022361872, u'12:02'),
(3, [-91.73610910364329, 46.58834277058802], 2.182985003705143, 0.37922035507660834, 3, -91.73610910364329, 46.58834277058802, u'12:03'),
(4, [-88.74246771509985, 46.15461041370514], 2.847861592720326, 0.6453984129693076, 4, -88.74246771509985, 46.15461041370514, u'12:04'),
(5, [-88.39081946312274, 47.42135644145347], 3.038898807952548, 0.7348904666573336, 5, -88.39081946312274, 47.42135644145347, u'12:05'),
(6, [-89.14675937270161, 48.526768055705695], 2.7076341063329634, 0.5834049208607849, 6, -89.14675937270161, 48.526768055705695, u'12:06'),
(7, [-90.56562763766716, 47.32958486445008], 2.915101303346966, 0.676234679810017, 7, -90.56562763766716, 47.32958486445008, u'12:07'),
(8, [-93.81217334723218, 46.916116897213875], 1.9081371199688988, 0.2897405607664292, 8, -93.81217334723218, 46.916116897213875, u'12:08'),
(9, [-91.97128321056668, 45.9718603576183], 2.221744769908184, 0.3928063220556296, 9, -91.97128321056668, 45.9718603576183, u'12:09'),
(10, [-90.70904602560582, 45.73189770788447], 1.0070513913079986, 0.08070369208883092, 10, -90.70904602560582, 45.73189770788447, u'12:10')],
dtype=[('OBJECTID', '<i4'), ('SHAPE', '<f8', (2,)), ('SHAPE_Length', '<f8'), ('SHAPE_Area', '<f8'), ('VEHICLEID', '<i4'), ('NEAR_X', '<f8'), ('NEAR_Y', '<f8'), ('TIME', '<U50')])
>>>
To get the first row and second column value as in your example, you can use:
>>> print(array[0][1])
[-90.76623071 48.059191 ]
>>>
Here is an example of a query within the OBJECTID
column:
>>> np.where(array["OBJECTID"] > 5 )
(array([5, 6, 7, 8, 9]),)
>>>