How to access just one specific value in a row

2043
6
03-27-2014 11:44 AM
JoannaLaroussi
New Contributor
I am looking for a way to compare  values in my fieldA. Specifically I would like to compare my last value to the one in the row above. I used GetCount to know how many rows I have. So for example if GetCount returns that there is 10 rows, I would like to compare numeric values from row 10 and 9 in a fieldA. For some reason I have problem with accessing values with a specific index: I can access all values but not just one. I tried to modify code from http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000

with arcpy.da.SearchCursor(fc, ['fieldA',]) as cursor:
    for row in cursor:
        print(row)

to something like

with arcpy.da.SearchCursor(fc, ['fieldA',]) as cursor:
    for row in cursor:
        myValue=row[10]
print (myValue)

but it does not work.
Did anybody figured out how to access just one specific value, not all of them?
Tags (2)
0 Kudos
6 Replies
AdamCox1
Occasional Contributor II
0 Kudos
EdFarrell
New Contributor III
Not the most elegant solution, but since you only have 10 rows...

i = 0
lastvalue = []
with arcpy.da.SearchCursor(fc, ["fieldA"]) as cursor:
    for row in cursor:
        lastvalue.append(row[0])  # keep track of all your values so you have a lookup table.
        if i == 9: # this is the last row in the table, i.e. row 10
            if row[0] == lastvalue[i-1]:  # compare the last row in the table to row 9.
                print('current row equals the previous row')

        i += 1
0 Kudos
T__WayneWhitley
Frequent Contributor
jlaroussi...just a quick note, at the 10.1 help reference you list, there is a section "Accessing and setting field values" and the very 1st sample code shows the following (shortened here) - I think you need to pay careful attention to the comment 'Access and print the row values by index position.' (which appears in my browser not to be justified left with the other comments so maybe you missed it or didn't fully understand):
with arcpy.da.SearchCursor(fc, ['STATE_NAME', 'POP2000']) as cursor:
    for row in cursor:
 # Access and print the row values by index position.
        #   state name: row[0]
        #   population: row[1]
        #
        print('{0} has a population of {1}'.format(row[0], row[1]))


The 'index position' is in this example the position within the list ['STATE_NAME', 'POP2000']; in other words, not related to the rows or fields in your attribute table in the way you may be thinking.  The row object returned in each iteration (for row in cursor) is a single pair of state name and population values - so row[10] would have no meaning, unless of course you had 11 field names in the list to return values for....in that case, for a particular row, you'd ask for the last value from the 11th field name you specify with row[10].

So, if you wanted to make this dynamic, returning only the last 2 row values regardless of the number of rows, you could leverage the count you said you are retrieving --- untested code below, but I think you could use that count (also dynamically fetched) something similar to the following:
# your count value as a var named 'count'
count = arcpy.GetCount.... etc.

with arcpy.da.SearchCursor(fc, ['fieldA']) as cursor:
     row = cursor.next()
     for i in range(count):
          if i <= count - 2:
               print('row {0} has a value of {1}'.format(str(i + 1), row[0]))
          row = cursor.next()
0 Kudos
ChrisSnyder
Regular Contributor III
How about:

listOfFieldValues = [r[0] for r in arcpy.da.SearchCursor(myTbl, ["FieldA"])]
numberOfFieldValues = len(listOfFieldValues)
lastFieldValue = listOfFieldValues[-1]
secondToLastFieldValue = listOfFieldValues[-2]
0 Kudos
T__WayneWhitley
Frequent Contributor
yep, ye ol' list comprehension, certainly worth a vote!  Personally, I would have preferred compiling a dictionary to have a key lookup for any value I like, ah but this user needs to learn how to fish 1st, what do you think?

Happy Friday, Chris!

PS- my 1000th post, for what that is worth!
0 Kudos
benberman
Occasional Contributor
something like this:

list =sorted([row.getvalue(fieldname) for row in arcpy.SearchCursor(inputdata,query...fields,sort)])
print list[-2:]
0 Kudos