[Tutor] Unique Items in Lists

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jan 26 23:32:32 CET 2005



On Wed, 26 Jan 2005, Srinivas Iyyer wrote:

> I have a list with 4 columns and column1 elements are unique.  I wanted
> to extract unique elements in column3 and and place the other elements
> of the column along with unique elements in column 4 as a tab delim
> text.
>
> Table:
>
> col1    col2    col3   col4
> A       Apple     5    Chennai
> B       Baby     11    Delhi
> I       Baby*     1    Delhi
> M       Dasheri+  5    Mumbai
> K       Apple     12   Copenhagen


[Meta: we seem to be getting a run of similar questions this week. Scott
Melnyk also asked about grouping similar records together:
http://mail.python.org/pipermail/tutor/2005-January/035185.html.]


Hi Srinivas,

I see that you are trying to group records based on some criterion.  You
may find the problem easier to do if you fist do a sort on that criterion
column: that will make related records "clump" together.


For your sample data above, if we sort against the second column, the
records will end up in the following order:

###
A       Apple     5    Chennai
K       Apple     12   Copenhagen
B       Baby      11   Delhi
I       Baby      1    Delhi
M       Dasheri   5    Mumbai
###


In this sorting approach, you can then run through the sorted list in
order.  Since all the related elements should be adjacent, grouping
related lines together should be much easier, and you should be able to
produce the final output:

###
Apple     A,K     5,12    Chennai,Copenhagen
Baby      B,I     1,11    Delhi
Dasheri   M       5       Mumbai
###

without too much trouble.  You can do this problem without dictionaries at
all, although you may find the dictionary approach a little easier to
implement.




> A dictionary option does not work

A dictionary approach is also very possible.  The thing you may be stuck
on is trying to make a key associate with multiple values.  Most examples
of dictionaries in tutorials use strings as both the keys and values, but
dictionaries are more versatile: we can also make a dictionary whose
values are lists.


For example, here is a small program that groups words by their first
letters:

###
>>> def groupAlpha(words):
...     groups = {}
...     for w in words:
...         firstLetter = w[0]
...         if firstLetter not in groups:
...             groups[firstLetter] = []
...         groups[firstLetter].append(w)
...     return groups
...
>>> groupAlpha("this is a test of the emergency broadcast system".split())
{'a': ['a'],
 'b': ['broadcast'],
 'e': ['emergency'],
 'i': ['is'],
 'o': ['of'],
 's': ['system'],
 't': ['this', 'test', 'the']}
###


If you have more questions, please feel free to ask.



More information about the Tutor mailing list