I am having a problem with my nested loops. I am getting an output, but it is duplicating each output for the number of fields in my data set.
for dat in data_body:
x = float(dat[5])
y = float(dat[6])
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(x,y)
feature.SetGeometry(point)
for i, d in enumerate(dat):
for j, field in enumerate(new_fields):
if i == j:
feature.SetField(field, d)
layer.CreateFeature(feature)
For my datasets, the list of lists data_body has 13 rows of data and 55 columns (i.e. 55 positions). For my list new_fields I have a 55 values, which correspond to the columns of data_body list of lists. Thus, in my code block for the object feature.SetField(field, d). I should have each value of the data_body correspond to each unique field, as long as rows match each other, i.e. i and j. However, rather getting an object back with 13 rows of data corresponding with column values corresponding to the values in new_fields. I get 13 * 55 values, i.e. 715 rows, where values are missing for ~ 50% of the data. My output data table kind of looks like a triangle of data.
I am not sure if my question makes sense, but if it does, any help with my nested looping strategies would be helpful. i think my issue is my conceptualization of what my loop is actually doing, as well as what my if statement might be doing.
As avasal said, you want to iterate over dat and new_fields at the same time, and the easiest way to do this is to use
zip: