[Tutor] Unique elements mapping

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Mar 25 20:28:10 CET 2005



On Fri, 25 Mar 2005, Srinivas Iyyer wrote:


> Name    State
> Drew    Virginia
> Noel    Maryland
> Niki    Virginia
> Adams   Maryland
> Jose    Florida
> Monica  Virginia
> Andrews Maryland
>
>
> I would like to have my ouput like this:
>
> Virginia :  Drew,Niki,Monica
> Maryland:   Noel,Adams, Andrews
> Florida:  Jose


Hi Srinivas,

Sean showed you how to fix the bugs in the program; I wanted to highlight
one particular bug that you were running into:


Let's look at your program again:

> for line in my_list:
>         key = line.split('\t')[0]
>         val = line.split('\t')[1]

This code is trying to extract a key and value out of every line in
my_list.  This part actually looks ok.

One of the bugs, though, is that the code doesn't collect those keys and
values up: it's only holding on to one particular key and one particular
value at a time.  When we say 'key' or 'val', we're only talking about one
particular name or state.  And this means that we're probably dropping
things on the floor.


One way we can fix this is to use a container for all the keys and all the
values.  We can plunk each new key and value into their respective
containers:

######
keys = []
vals = []
for line in my_list:
    key = line.split('\t')[0]
    val = line.split('\t')[1]
    keys.append(key)
    vals.append(val)
######



By the way, we can make this into a function, to make it easier to test
out:

######
def getKeysAndValues(my_list):
    keys = []
    vals = []
    for line in my_list:
        key = line.split('\t')[0]
        val = line.split('\t')[1]
        keys.append(key)
        vals.append(val)
    return keys, vals
######


As a function, this is easier to test since we can feed the function some
sample data, and see if it breaks:

###
>>> assert (getKeysAndValues(["hello\tworld", "goodbye\tworld"]) ==
...            (["hello", "goodbye"], ["world", "world"]))
>>>
###

And since Python doesn't complain here, we're reasonably sure that it's
doing the right thing.


I want to emphasize that getKeysAndValues() is probably not exactly what
you want: Sean's solution with the dictionary's setdefault()  stuff in his
previous post sounds right.

But keeping things in functions is nice because we can later just test
specific parts of a program to make sure they're happily working.


Please feel free to ask more questions about this;  we'll be happy to
help.



More information about the Tutor mailing list