Calculate Field with Python

1001
8
03-24-2021 04:28 AM
Labels (3)
by
New Contributor III
uniqueList = []
def isDuplicate(inValue):
if inValue in uniqueList:
return 1
else:
uniqueList.append(inValue)
return 0

Above coding to output 0 1.
I'd like to output 1 1.

1 Solution

Accepted Solutions
MVP Frequent Contributor

I'd probably run a searchCursor to generate a list of unique values, then run the field calculator using that list of pregenerated unique values.

8 Replies
MVP Frequent Contributor

Which field are you calculating on? Also unsure of the logic of that code compared to your expected output - doesn't make sense to me.

Anyway, as the code is executed on a per-row basis with the function, unique_list would have to be set to a global.

``````uniqueList = []
def isDuplicate(inValue):
global uniqueList
if inValue in uniqueList:
return 1
else:
uniqueList.append(inValue)
return 0``````
by
New Contributor III

MVP Frequent Contributor

Are you saying the top picture is what you want - that makes sense to me - rather than the bottom picture?

looking at the code, I don't see how you got those results in the bottom picture either.  Did you try the code I suggested?

by
New Contributor III

The picture below is the printout I want. I used your code.

I'd like to express all duplicate names by 1.

by
New Contributor III

MVP Frequent Contributor

I'd probably run a searchCursor to generate a list of unique values, then run the field calculator using that list of pregenerated unique values.

by
New Contributor III

Esri Esteemed Contributor

It may be easier to do this with a script rather than the Field Calculator.  Ex:

``````import arcpy

table = r"C:\TEMP\PYTHON\Test.gdb\XY"

nameDict = {}

with arcpy.da.SearchCursor(table, ["NAME"]) as cursor:
for row in cursor:
if row[0] not in nameDict.keys():
nameDict[row[0]] = 0
else:
nameDict[row[0]] = 1
del cursor

with arcpy.da.UpdateCursor(table, ["NAME", "Field"]) as cursor:
for row in cursor:
row[1] = nameDict[row[0]]
cursor.updateRow(row)
del cursor``````

Result: