tags:

views:

126

answers:

2

Hello All-

I have python code below that will loop through a table and print out values within a particular column. What is not shown is the form in which the user selects a Feature Layer. Once the Feature Layer is selected a second Dropdown is populated with all the Column Headings for that Feature and the user chooses which Column they want to focus on. Now within the python script, I simply print out each value within that column. But I want to store each value in a List or Array and get Distinct values. How can I do this in Python?

Also is there a more efficient way to loop through the table than to go row by row? That is very slow for some reason.

many thanks

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.AddToolbox("E:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Declare our user input args
input_dataset = sys.argv[1] #This is the Feature Layer the User wants to Query against
Atts = sys.argv[2]          #This is the Column Name The User Selected

#Lets Loop through the rows to get values from a particular column          

fc = input_dataset

gp.AddMessage(Atts)

rows = gp.searchcursor(fc)
row = rows.next()
NewList = []

for row in gp.SearchCursor(fc):
    ##grab field values
    fcValue = fields.getvalue(Atts)
    NewList.add(fcValue)
+2  A: 

You can store distinct values in a set:

>>> a = [ 1, 2, 3, 1, 5, 3, 2, 1, 5, 4 ]
>>> b = set( a )
>>> b
{1, 2, 3, 4, 5}
>>> b.add( 5 )
>>> b
{1, 2, 3, 4, 5}
>>> b.add( 6 )
>>> b
{1, 2, 3, 4, 5, 6}

Also you can make your loop more pythonic, although I'm not sure why you loop over the row to begin with (given that you are not using it):

for row in gp.searchcursor( fc ):
    ##grab field values
    fcValue = fields.getvalue(Atts)
    gp.AddMessage(fcValue)

And btw, """ text """ is not a comment. Python only has single line comments starting with #.

poke
Aye. Use `set` and you'll be all set. +1.
Manoj Govindan
@poke- Hi poke, I changed my code to reflect your adjustments and I get an error. Do you see any problems with it above? Also I left out the set() for now until I get the loop working
Josh
What I was wondering is (a) why you iterate over `row`, (b) what `fields` is (as you didn't define it in the shown code, but I guessed it came from somewhere else) and (c) you might get problems now as you call `searchcursor` twice; it's possible that this conflicts, so better remove the first call.
poke
BTW, it's OK to use `""" text """` to put comments in Python code -- not common, but it works and is one way to do multi-line comments. Since they are expressions, doing so might impact execution to some degree depending on where they're placed (although the optimizer probably gets rid of most of them).
martineau
+1  A: 

One way to get distinct values is to use a set to see if you've seen the value already, and display it only when it's a new value:

fcValues = set()
for row in gp.searchcursor(fc):
    ##grab field values
    fcValue = fields.getvalue(Atts)
    if fcValue not in fcValues:
        gp.AddMessage(fcValue)
    fcValues.add(fcValue)
Steven Rumbalski