import collections def has_duplicates(list_of_values): value_dict = collections.defaultdict(int) for item in list_of_values: value_dict[item] += 1 return any(val > 1 for val in value_dict.itervalues()) if has_duplicates(my_list): print "The list provided contains duplicate values" else: print "The list provided does not contain duplicate values."
def FindDuplicates(in_list): unique = set(in_list) for each in unique: count = in_list.count(each) if count > 1: print 'There are duplicates in this list' return True print 'There are no duplicates in this list' return False if __name__ == '__main__': # test it a = [8, 64, 16, 32, 4, 24] b = [2,2,3,6,78,65,4,4,5] FindDuplicates(a) FindDuplicates(b)
Cur = arcpy.da.SearchCursor(InFc, ["Field"]) # where Field is the column to count duplicates FDict = {} for r in Cur: f = r[0] if f in FDict: FDict+= 1 else: FDict = 1 del Cur
Something like this should do the trick:def FindDuplicates(in_list): unique = set(in_list) for each in unique: count = in_list.count(each) if count > 1: print 'There are duplicates in this list' return True print 'There are no duplicates in this list' return False
>>> a = ( 2, 8, 64, 16, 32, 4, 16, 8) >>> b = set(a) >>> if len(b) < len(a): ... print "contains duplicates" ... else: ... print "no duplicates found:" >>> contains duplicates
If anyone can help me with this I really would appricate it. I'm a novice
My task is to create a script a that examines a list of numbers (for example, 2, 8, 64, 16, 32 4, 16, 😎 to determine whether it contains duplicates. The script should print a meaningful result, such as "The list provided contains duplicate values" or "The list provided does not contain duplicate values."
Thanks,
Larry
list = ( 2, 8, 64, 16, 32, 4, 16, 8) if len(list) <> len(set)list): print "The list provided contains duplicate values"
I didn't know about "set" before, so nice post. I saw it because this popped up in my geonet email, but notices a syntax error above, and then expanded for a non-dupe list. fwiw.
list = ( 2, 8, 64, 16, 32, 4, 16, 8)
if len(list) <> len(set(list)):
print("the list provided contains duplicate values")
else:
print("no dupes")
list = (1, 2, 3, 4, 5)
if len(list) <> len(set(list)):
print("the list provided contains duplicate values")
else:
print("no dupes")
edit: ....oops...I missed Doug's and Dan's examples. shows you what happens when you go to the bottom of the geonet comments first.