On 04/12/13 03:51, Byron Ruffin wrote:

is doing this.  Also, it seems to be referencing chars when variable
lastName is an item in a list.


Thats because you are looping over the name.
Loops work on any iterable or sequence.
A string is a sequence of chars so you can loop over a
string as easily as over a list.

What you needed to do was lift Steve's code up a level
to your outer loop.

Here is all my code:

def createList2(filename):
     List = []
     senateInfo2 = {}

     info = open( filename, "r" )
     for line in info:

You could just do
       for line in open(filename):

         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()

You should have moved that up before the for line...

         for name in lastName:

You don't need this loop, you have the name, you
don't want to read its characters.

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

So move this out a level and change name to lastname:

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

         senateInfo2[lastName] = state

That then leads onto Oscar's suggestion to store a list of firstname/state tuples rather than just storing the last
state seen.

     info.close()
     return senateInfo2

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to