[Tutor] Unique Items in Lists

Brian van den Broek bvande at po-box.mcgill.ca
Thu Jan 27 08:06:21 CET 2005


Srinivas Iyyer said unto the world upon 2005-01-27 01:17:
> Dear Jacob, thank you for your suggestion.
> 
> however, i think my question was not clear. what i
> meant to ask in my previous question was, how to know
> which elements repeated and how many times they were
> repeated. 
> 
> while my question was flying, i did a small test:
> 
> took a list:
> 
>>>>a
> 
> [1, 1, 2, 3, 4, 2, 2]
> 
> wanted to know which elements repeated and how many
> times:
> 
> for i in range(len(a)):
> 	for k in range(len(a)):
> 		if i != k:
> 			if a[i] == a[k]:
> 				print a[i]
> 				break

<SNIP>

> 
> In this very huge list (:-) kidding) of 7 elements, it
> is easy know by the above primitive code that there 1
> s repeated two times, and 2 s repeated three times and
> finally 1 and 2 numbers are repeated in list a:
> 
> With sets option i get a list of unique elements, but
> by no means i get to know which elements were repeated
> and how many times.  With the above primitive code , I
> know that 1 and 2 are peated.  However, in case where
> there are thousands of entries, how can I get to kno
> wich elements got repeated and how many times.  
> 
> Do you see any flaws in this code, how can that be
> made to look more pyhonian way. 
> 
> Hope my question is clear now and appreciate your
> suggestions.
> 
> Thank you in advance
> Srini

<SNIP>

Hi Srini,

for the task of finding out which items are repeated and how many 
times, I'd do this:

<code>
def dups_in_list_report(a_list):
     '''Prints a duplication report for a list.'''

     items_dict = {}

     for i in a_list:
         if i in items_dict:
             items_dict[i] = items_dict[i] + 1
         else:
             items_dict[i] = 1

     for key in items_dict.copy():   # Try it without the .copy()
         if items_dict[key] == 1:    # and see what happens.
             del items_dict[key]

     dict_keys = items_dict.keys()
     dict_keys.sort()

     for key in dict_keys:
         print '%s occurred %s times' %(key, items_dict[key])

f = [1,1,2,3,3,3,3,4,4,4,4,4,4,4,5]

dups_in_list_report(f)
</code>

And, now that I get back on-line, I see that Chad posted the same 
basic idea. But, perhaps the extra stuff here is of use, too.

HTH,

Brian vdB



More information about the Tutor mailing list