1

Is there any way to create a list based on the number of characters of the files? My current list goes like that

 rasterList  = arcpy.ListRasters("*speed*","TIF")

My files are speed01 speed02 ..... speed01500

I want for example only speed01000 to speed 01500 If I could apply a wild card with 10 number of characters this would be solved easily

  • 2
    This sounds like pure Python so I think you should research/ask your question at Stack Overflow. – PolyGeo Jul 28 '18 at 13:31
4

You can solve this with list comprehension and the len function:

speed = ['speed01','speed02','speed01000','speed01500']
speed = [i for i in speed if len(i)==10]

Result:

>>speed
['speed01000', 'speed01500']

There are other ways to divide the list, for example see: How do you split a list into evenly sized chunks?:

numbers = [i for i in range(10)] #Create some data
numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
chunksize = 4 #Change
numbers = [numbers[i:i + chunksize] for i in xrange(0,len(numbers),chunksize)] #Change xrange to range for python3/ArcGIS Pro

numbers
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
for g in numbers:
   print g

[0, 1, 2, 3]
[4, 5, 6, 7]
[8, 9]
| improve this answer | |
  • Thank you. The problem here is that the listRasters list cant hold more than 1000 files. So initially I cannot have a list for all my files. My problem is how to split those 1500 rasters in 1000 and 500. – GeoF Jul 28 '18 at 14:18
  • 1
    @GeoF why cant it hold more? Do you get an error message? – BERA Jul 28 '18 at 14:35
  • No but when I print the list only the first 1000 show up. I think 1000 is the limit in arcpy listRasters function – GeoF Jul 28 '18 at 14:45
  • 1
    Maybe it is not printing all of them. Try print len(speed), what is the output? – BERA Jul 28 '18 at 14:46
  • Hmm that seems to work. Strange but thanks a lot. Maybe they are not all printed. But either way I need to call cellstatistics function afterwards which I think has a limit of 1000 rasters. Am I correct? If thats the case I will try to use list comprehension as you suggested to divide my list – GeoF Jul 28 '18 at 14:59

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