kurdtc
kurdtc

Reputation: 1621

Matching strings in for loop in Python

I have a pandas dataframe df which looks like this

enter image description here

df = {'Regions': {0: 'REGION1', 1: 'REGION2'}, 'x': {0: '1249-43,1269-12,1280-12', 1: '1267-12,1269-12,1280-12'}}

and a list of raster files called rasters

rasters = 'SI_1206-33_50cm.tif', 'SI_1249-43_50cm.tif', 'SI_1269-12_50cm.tif', 'SI_1267-12_50cm.tif', 'SI_3865-17_50cm.tif' 

What I'd like to do is to create a new list of all entries in rasters that match the strings in df.x per region.

for index, row in df.iterrows():
    region = row['Regions']
    tiffs = row['x']
    rasterlist = []
    for raster in rasters:
        if raster in tiffs:
            rasterlist = rasterlist.append(raster)
            print(rasterlist)

As a result when iterating over rasters, I am attempting to get for the first iteration a rasterlist containing 'SI_1249-43_50cm.tif' and 'SI_1269-12_50cm.tif' for REGION1 and for the second iteration a rasterlist containing only 'SI_1267-12_50cm.tif' for REGION2. The list rasterlist I would like to use as input for further processing using the MosaicToNewRaster_management fucntion in arcpy.

What does not seem to work with this code is the pattern matching, I get an empty rasterlist variable for each iteration. I assume this is the case because the different list items in df.x are separated by a comma and the in function does not seem to work. This is where I am stuck and hoping to get some input.

Upvotes: 1

Views: 3430

Answers (1)

Nick Chapman
Nick Chapman

Reputation: 4634

You are checking in the wrong direction. You are using

if raster in tiffs

but tiffs is just something like '1249-43,1269-12,1280-12' which obviously none of the rasters are in. You need to split apart the list of tiffs and check in the reverse direction to see if any of the tiffs are in the rasters.

tiffs = row['x'].split(',')
raster_list = []
for raster in rasters:
    for tiff in tiffs:
        if tiff in raster:
            raster_list.append(raster)
            # Assuming each tiff matches with only one raster
            # you can break to save some time here.
            break
print(raster_list)

If you could tell us more about the mapping between the rasters and tiffs then there's probably something more efficient that can be done using dicts or sets.

Upvotes: 2

Related Questions