Search code examples
pythonfunctionfor-looparcpy

Passing a list to another function


I'm using a python module https://gis.stackexchange.com/a/5943/16793 to export a list of feature classes within an SDE database.

 import os, csv, arcpy, arcplus
>>> fcs = arcplus.listAllFeatureClasses("Database Connections\\Connection to oracle.sde\\BASINS.ACF")

The output is a list:

[u'BASINS.ACF_FL_SUB', u'BASINS.ACF_CHATTAHOOCHEE_BASIN', u'BASINS.ACF_CHIPOLA_BASIN', u'BASINS.ACF_CHIPOLA_AL']

In order to pass this list to another function, I've prepended a string to each element in the list:

mylist = ['Database Connections\\Connection to oracle.sde\{0}'.format(i) for i in fcs]

which looks like:

print mylist[0]
Database Connections\Connection to oracle.sde\BASINS.ACF_FL_SUB

I'd like to pass this list to another function arcpy.ListFields(dataset) which will return the fields of each feature class:

fn = [f.name for f in arcpy.ListFields(mylist[0])]

>>> print fn
[u'OBJECTID', u'HUC', u'BASIN', u'NAME', u'ACRES', u'SHAPE', u'SHAPE.AREA', u'SHAPE.LEN']

I'm trying to figure out how to pass the list in fcs to the function arcpy.ListFields and write the results to csv file, but the structure of the loop needed is really giving me trouble. I'm a novice at this and the Python documentation is getting me turned around. Any pointers would be helpful.

_________________________ETC__________________________________________________

@Tony Your solution worked great. Although I try to use listAllFeatureClasses on the larger geodatabase, I don't have insufficient privileges to read some of the attributes, which gives an IOError: Database Connections\Connection to oracle.sde\LAND.LANDS\LAND_POINTS does not exist. I'm working on how to handle this, and continue to the next feature class in the list. Maybe Try/Continue?


Solution

  • To call the arcpy.ListFields for every item in a list :

     fns = [[f.name for f in arcpy.ListFields( list_entry )] for list_entry in mylist]
    

    This will give you a list of lists, where fns[0] are the functions for entry mylist[0]

    what might be easier to work with is a dictionary :

     fns_dict = dict( [ (list_entry, [f.name for f in arcpy.ListFields( list_entry ) ] )
                                           for list_entry in mylist ] )
    

    Using the data in your example :

     fns_dict["BASINS.ACF_FL_SUB"] should be 
     [u'OBJECTID', u'HUC', u'BASIN', u'NAME', u'ACRES', u'SHAPE', u'SHAPE.AREA', u'SHAPE.LEN']