0

There are the following shapefiles in workspace.

L01-1983_01.shp
L01-1983_02.shp
...
L01-1983_46.shp
L01-1983_47.shp
L01-1984_01.shp
L01-1984_02.shp
...
L01-1984_46.shp
L01-1984_47.shp
...
L01-2012_01.shp
L01-2012_02.shp
...
L01-2012_46.shp
L01-2012_47.shp
L01-2013_01.shp
L01-2013_02.shp
...
L01-2013_46.shp
L01-2013_47.shp

File name has the following rule.

L01-(year)_(city code).shp

(year) is 1983~2013 and (city code) is 01~47

I want to make lists of each year below.

list_1983=["L01-1983_01.shp","L01-1983_02.shp",...,"L01-1983_46.shp","L01-1983_47.shp"]
list_1984=["L01-1984_01.shp","L01-1984_02.shp",...,"L01-1984_46.shp","L01-1984_47.shp"]
...
list_2012=["L01-2012_01.shp","L01-2012_02.shp",...,"L01-2012_46.shp","L01-2012_47.shp"]
list_2013=["L01-2013_01.shp","L01-2013_02.shp",...,"L01-2013_46.shp","L01-2013_47.shp"]

Below is my script.

from arcpy import *
env.workspace="C:\LandPrice(1983-2013)"
list_year=["list_%d" % x for x in range(1983,2014)]
year=1983
for i in list_year:
    i=ListFeatureClasses("*year*")
    year=year+1

But it doesn't work well.

How can I make lists of each year?

  • 1
    What error messages (or incorrect results) are you getting? – Erica Jun 11 '14 at 14:01
1

I suggest using a dictionary, which is essentially a list but allows you to define an arbitrary index instead of using integers to access the information. That gives somewhat cleaner code, and essentially will create a "list of lists." Individual lists can be accessed individually.

import arcpy
from arcpy import *

env.workspace = 'c:\\temp'

list_shp = {}

for year in range(1983,2014):
   list_shp[year] = ListFeatureClasses('*{0}*'.format(year))

For example: after that loop finishes, list_shp[1985] is the list of shapefiles associated with the year 1985.

  • Can list comprehensions be used to populate a dictionary? – Paul Jun 11 '14 at 14:54
  • Apparently so -- I tested it with dummy data in my temp folder and it appears to be working. I was mildly surprised. – Erica Jun 11 '14 at 14:57
0

If you are looking to build a list of existing shape files, you can easily generate this outside of ArcGIS (which always adds extra overhead and slows things down) by using python objects to look for all files ending in .shp.

import fnmatch, os
matches = []
Year = 2000
searchPattern = "*" + str(Year) + "*.shp" # <== Use this line to select a single year
#searchPattern = "*.shp" # <== Use this line to select all shape files

for root, dirnames, filenames in os.walk("C:\\Temp\\"):
    for filename in fnmatch.filter(filenames, searchPattern): 
        #For just the file name use this line
        matches.append(filename)

        #For full path use this line
        #matches.append(os.path.join(root, filename))

print matches

This will give you all of the shape files that actually exist in the directory not not just a listing of what you expect to be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.