I'm trying to create an update cursor which will take the count of the number of like features in a field and then use that number in a random number generator. The field would look something like that shown below:
**Field**
X
X
X
X
X
Y
Y
Y
Y
Y
Z
Z
As had been suggested by someone on here previously, I tried using collections however, I find that the result is not always accurate with the count total. For example I may have values greater than the total number of like features. I managed to get something working however, now I need something which constrains the random number without replacement and I feel the only way to do this is using collections. My code is shown below:
def sumdict(listvals):
ddict = collections.defaultdict(int)
for val in listvals:
ddict[val]+=1
return ddict
dic = sumdict(row[0] for row in arcpy.da.SearchCursor(MyFC, (Field1)))
with arcpy.da.UpdateCursor(MyFC, (Field1, Field2)) as cursor:
for row in cursor:
row[1] = sample(1, dic[row[0]])
cursor.updateRow(row)
Below is an example of what the output should look like, naturally the random numbers will vary.
Field1 Field2
X 2
X 3
X 1
X 5
X 4
Y 3
Y 5
Y 4
Y 2
Y 1
Z 2
Z 1