[Tutor] need a hint

Byron Ruffin byron.ruffin at g.austincc.edu
Wed Dec 4 04:51:12 CET 2013


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 win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
Already seen s
Already seen s
Already seen k
Already seen r
Already seen o
Already seen e
Already seen i
Already seen n
Already seen l
Already seen n
Already seen e
Already seen l
Already seen r
Already seen o
Already seen s
Already seen s
Already seen o
Already seen n
Already seen l
Already seen s
Already seen n
Already seen l
Already seen t
Already seen l
Already seen k
Already seen i
Already seen r
Already seen n
Already seen l
Already seen u
Already seen e
Already seen n
Already seen l
Already seen e
Already seen h
Already seen e
Already seen t
Already seen e
Already seen e
Already seen n
Already seen e
Already seen l
Already seen i
Already seen l
Already seen i
Already seen r
Already seen a
Already seen e
Already seen e
Already seen o
Already seen e
Already seen h
Already seen e
Already seen a
Already seen t
Already seen o
Already seen n
Already seen e
Already seen r
Already seen n
Already seen e
Already seen r
Already seen r
Already seen l
Already seen e
Already seen l
Already seen e
Already seen n
Already seen o
Already seen n
Already seen r
Already seen a
Already seen s
['John Cornyn (R)', 'Ted Cruz (R)']
New Mexico

Here is all my code:

def createList( filename ):
    # print( filename )
    senateInfo = {}
    try:
        info = open( filename, "r" )

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

            if state in senateInfo:                     # Adding another
senator.
                # Create a list of the both senators from that state.
                incumbent = senateInfo[state]
                senators = [ incumbent, senator ]
                senateInfo[state] = senators

            else:
                senateInfo[state] = senator

        #print( senateInfo )

        info.close()
    except:
        print( filename, " did not open! qUITTING." )
    return senateInfo

def createList2(filename):

    List = []
    senateInfo2 = {}

    info = open( filename, "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]

        already_seen = set()

        for name in lastName:
            if name in already_seen:
                print("Already seen", name)
            else:
                already_seen.add(name)







        senateInfo2[lastName] = state




    info.close()

    return senateInfo2

def test( state, senatorsInfo ):
    print( senatorsInfo[state] )

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

def main():
    usSenators = createList( "USSenators.txt" )
    usSenators2 = createList2( "USSenators.txt" )
    test( "Texas", usSenators )
    test2("Udall", usSenators2 )


main()




On Tue, Dec 3, 2013 at 7:54 PM, Steven D'Aprano <steve at pearwood.info> wrote:

> On Tue, Dec 03, 2013 at 11:55:30AM -0600, Byron Ruffin wrote:
> > What I am having trouble with is finding a way to say:  if lastName
> appears
> > more than once, print something.
> >
> > I ran a bit of code:
> > For x in lastname
> >   If lastname = udall
> >    Print something
>
> You most certainly did not run that. That's not Python code. Precision
> and accuracy is vital when programming. Please tell us what you
> *actually* ran, not some vague summary which may or may not be in the
> right ballpark.
>
> Copy and paste is your friend here: copy and paste the block of code you
> ran, don't re-type it from memory.
>
> > This prints x twice.
> >
> > I think what I might be hung up on is understanding the ways that I can
> use
> > a loop.  I know I need to loop through the list of names, which I have,
> and
> > set a condition dor the apppearance of a string occurring more than once
> in
> > a list but I don't know how to translate this to code.   How do I say: if
> > you see it twice, do something?
>
> How do you know you've seen it twice? You have to remember the things
> you've seen before. The best way to do this is with a set, if possible,
> or if not, a list.
>
> already_seen = set()
> for name in last_names:
>     if name in already_seen:
>         print("Already seen", name)
>     else:
>         already_seen.add(name)
>
>
>
> Here's another way, not recommended because it will be slow for large
> numbers of names. (But if you only have a few names, it will be okay.
>
> for name in last_names:
>     n = last_names.count(name)
>     print(name, "appears %d times" % n)
>
>
> Can you combine the two so that the number of times a name appears is
> only printed the first time it is seen?
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131203/317af6b9/attachment-0001.html>


More information about the Tutor mailing list