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 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">64</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">16</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">32</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">16</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token"><></SPAN> len<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"the list provided contains duplicate values"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"no dupes"</SPAN><SPAN class="punctuation token">)</SPAN> list <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token"><></SPAN> len<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"the list provided contains duplicate values"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"no dupes"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
list = ( 2, 8, 64, 16, 32, 4, 16, 8) if len(list) <> len(set)list): print "The list provided contains duplicate values"
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
>>> 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
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
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
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
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)
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."
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.