[Tutor] need a hint

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Dec 4 11:22:07 CET 2013


Byron Ruffin <byron.ruffin <at> g.austincc.edu> writes:

> 
> 
> 
> 
> 
> I realize the code snippet was bad.  It was meant to be pseudo code.  I
was on my phone and far from pc. Anyway....
> I tried this:  already_seen = set()
> for name in last_names:
>     if name in already_seen:
>         print("Already seen", name)
>     else:
>         already_seen.add(name)
> 
> I am not seeing a pattern in the output to give me a clue as to why it is
doing this.  Also, it seems to be referencing chars when variable lastName
is an item in a list.  
> 
> Unexpected output:Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC
v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()"
for more information.
> >>> ================================ RESTART
================================>>> Already seen sAlready seen sAlready seen
kAlready seen rAlready seen oAlready seen eAlready seen i
> Already seen nAlready seen lAlready seen nAlready seen eAlready seen
lAlready seen rAlready seen oAlready seen sAlready seen sAlready seen
oAlready seen nAlready seen lAlready seen s
> Already seen nAlready seen lAlready seen tAlready seen lAlready seen
kAlready seen iAlready seen rAlready seen nAlready seen lAlready seen
uAlready seen eAlready seen nAlready seen l
> Already seen eAlready seen hAlready seen eAlready seen tAlready seen
eAlready seen eAlready seen nAlready seen eAlready seen lAlready seen
iAlready seen lAlready seen iAlready seen r
> Already seen aAlready seen eAlready seen eAlready seen oAlready seen
eAlready seen hAlready seen eAlready seen aAlready seen tAlready seen
oAlready seen nAlready seen eAlready seen r
> Already seen nAlready seen eAlready seen rAlready seen rAlready seen
lAlready seen eAlready seen lAlready seen eAlready seen nAlready seen
oAlready seen nAlready seen rAlready seen a

Ok, Stephen's attempt on introducing you to sets and iterators over nested
sequences was, lets say, ambitious (but it was hard for him to understand
your actual problem from your posted incomplete pseudocode snippets;
explaining your problem correctly is always key to good answers).
Now back to square one, this was your original code (removed a bit of
whitespace to make it more digestable):

def createList(state):   
    senateInfo = {}
    info = open("USSenators.txt", "r")

    for line in info:
        dataOnLine = line.split("\t")
        state = dataOnLine[0]
        senator = dataOnLine[1]
        nameSplit = dataOnLine[1].split(" ")

        if len(nameSplit) == 3:
            lastName = nameSplit[1]
        elif len(nameSplit) == 4:
            lastName = nameSplit[2]

        senateInfo[lastName] = state

    info.close()

    return senateInfo
   

def test(senator, usSenators):
    print(usSenators[senator])

   
def main():
    usSenators = createList("USSenators.txt")
    senator = input("Enter last name of Senator")
    test(senator, usSenators)
    

The improvement to the code you asked for was:
"For a duplicate name I would like to tell the user it is not a unique last
name and then tell them to enter first name and then return the state of
that senator." (while your code above simply overwrites pre-existing last
name entries in the dictionary.

What Oscar and me tried to tell you was:
1) if you want to use the first name of a senator to resolve ambiguous
situations, you need to extract that first name and store it along with the
other information. You succeeded in extracting last names and states
already, so doing this also for the first names should be feasible.

2) to be able to detect and report ambiguities we recommended to turn the
values of your dictionary into lists, which you use to store tuples of first
name and state. We provided you with code snippets to do this.
An ambiguous last name is detected then by a corresponding dictionary entry
with a length > 1 (you used len() in your code above, so you should know how
to implement the test.

So your final code should look roughly like this:

def createList(state):   
    senateInfo = {}
    info = open("USSenators.txt", "r")

    for line in info:
        dataOnLine = line.split("\t")
        state = dataOnLine[0]
        senator = dataOnLine[1]
        nameSplit = dataOnLine[1].split(" ")

        if len(nameSplit) == 3:
            lastName = nameSplit[1]
            # extract firstName
        elif len(nameSplit) == 4:
            lastName = nameSplit[2]
            # extract firstName

        # instead of senateInfo[lastName] = state,
        # which builds a simple state dictionary
        if lastName in senateInfo:
            senateInfo[lastName].append((firstName, state))
        else:
            senateInfo[lastName] = [(firstName, state)]
        # this builds a dictionary where values are lists of tuples,
        # with most lists ending up with a length of 1 element,
        # but some will have more
    
    # print that dict if you are unsure what it looks like
    info.close()

    return senateInfo
   

def test(senator, usSenators):
    print(usSenators[senator])

   
def main():
    usSenators = createList("USSenators.txt")
    senator = input("Enter last name of Senator")
    # test here if usSenators[senator] gives you an unambiguous answer
    # if so:
        test(senator, usSenators)
        # but you can really simplify this to a simple inline print
    # else:
    # ask user for a firstname
    # from usSenators[senator] get the first tuple, in which the first 
    # element == user_specified_firstname
    # print that tuple's second item (the state)
    # if the user_specified_firstname is nowhere in any tuple, ask again
    # or print an error message

Does that help?
Wolfgang





More information about the Tutor mailing list