โค้ด: เลือกทั้งหมด
def get_duplicates(arr):
dup_arr = arr[:]
for i in set(arr):
dup_arr.remove(i)
return list(set(dup_arr))
print get_duplicates([1,2,3,5,6,7,5,2])
#ผลที่ได้ [2, 5]
print get_duplicates([1,2,1,3,4,5,4,4,6,7,8,2])
#ผลที่ได้ [1, 2, 4]
โค้ด: เลือกทั้งหมด
list_a=['A','A',3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
#A
#5
โค้ด: เลือกทั้งหมด
for i in range(0, len(list_a) ):
for j in range(i + 1, len(list_a)):
if list_a[i] == list_a[j]:
print list_a[i]
#A
#5
โค้ด: เลือกทั้งหมด
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(list_a):
print "รายการที่ระบุมีค่าที่ซ้ำกัน"
else:
print "รายการที่ระบุมีค่าที่ไม่ซ้ำกัน"