I have used the label expression 'Label a related table' (one-to many) and it works just fine, but I would like to go one step further and 'count' repited values in the resulted label.
I just can't seem to figure out how to write it with python...
def FindLabel ([keyField], [FirstLabel]): import arcpy key1 = [keyField] # Key field in feature class key2 = "ID" # Key field in related table L = [FirstLabel] # Label field in feature class L2 = "Label2" # Label field in related table myDataTable = r"<path-to-related-table>" # Path to related table cur = arcpy.da.SearchCursor(myDataTable, [key2, L2]) for row in cur: if str(key1) == str(row[0]): L = L + " " + str(row[1]) return L
So the idea is to count row[1], for expample if there is 4 relations where the resulted label is "red red red green", I would like to show: "3 red, 1 green" .
Is this possible?
def FindLabel ( [GewässerID] , [OBJECTID]):
import arcpy
key_val = [GewässerID]
label_1 = [OBJECTID]
rel_table = 'path:/to/related/table'
rel_key_field = "GewässerID"
rel_label_field = "SegmentTyp"
where = f"{rel_key_field} = {key_val}"
rel_values = [r[0] for r in arcpy.da.SearchCursor(rel_table, [rel_label_field], where)]
distinct = list(set(rel_values))
numbers = [rel_values.count(d) for d in sorted(distinct)]
label_2 = ", ".join([f"{n} {v}" for n, v in zip(numbers, distinct)])
return f"{label_1}: {label_2}"
Thanks for your your answer!
Though it's not working quite as expected, since the function list(set(rel_values)) it separating the words into letters, and in the end counting the letter for each word... any suggestion on how I can fix that?
I have sadly very limited experience with python...
Hmm, it worked for my test data. Can you post your related table (the actual table or a screenshot of the label field)
I can see the logic in it... but it is not working on my data.
I can't post the table, but the label field is just text with values 'Inde' or 'Ude'.
I have also tried to use .count('Inde') values, and the result is still a separated list like "1, 1 ,0, 1" ...
Thank you very much for the help! It gave me some ideas to keep testing..