Calculate Field with Python

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

 

Hi~~~Please refer to the illustration.

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

qqqq.JPG 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
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.

View solution in original post

8 Replies
DavidPike
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
0 Kudos
KalSsin
New Contributor III

Thank you for your reply.AAAAA.JPG

0 Kudos
DavidPike
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?

0 Kudos
KalSsin
New Contributor III

I used your code.

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

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

0 Kudos
KalSsin
New Contributor III

XX.JPG

0 Kudos
DavidPike
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.

View solution in original post

KalSsin
New Contributor III

Thank you for your reply.

0 Kudos
JakeSkinner
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:

JakeSkinner_0-1616590859062.png