How to delete strings from a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Derbylad
    New Member
    • May 2010
    • 7

    How to delete strings from a list

    Right I've come across another problem in my coursework and cant seem to find anything similar in my lecture notes.
    The aim of the definition is to delete a string (i.e. an entire player) from a list (i.e. my list of members) based upon search criteria, such as the players name.

    So far i've only ever deleted single elements from a list not whole strings.
    Here's what i've got so far... My code for deleting a player is under the function definition "delete_player. ..".

    Code:
    class Member(object):
        '''a member in a squash team'''
    
    players = open ("players.txt","r")
    
    l= []
    for line in players:
    	line=line.strip()
    	x = line.split(";")
    	memb = Member()
    	memb.id= x[0]
    	memb.forename= x[1]
    	memb.surname= x[2]
    	memb.email= x[3]
    	memb.phone= x[4]
    	memb.divc= x[5]
    	memb.pointc= x[6]
    	memb.divp= x[7]
    	memb.pointp= x[8]
    	l.append(memb)
    players. close ()
    
    def new_player(member_id,forename,surname,email,phone_number,division_current,points_current,division_previous,points_previous):
        """
        Adds a new member to the register.
            member_id = a number representing the 
            forename = a players forename
            surname = a players surname
            email = a players email
            phone_number = a players phone number
            division_current = the current division the player is in
            points_current = the current points the player has
            division_previous = the previous division the player was in
            points_previous = the amount of points the player attained in the last round
        """
        l.append([member_id,forename,surname,email,phone_number,division_current,points_current,division_previous,points_previous])
    
    def delete_player(forename):
        """
        Delete a member from the list
            forename = a players forename (str)
        """
        for j in range(0,len(l)):
            x = l[j]
            if x[1] == forename:
                del l[j]
                break
    When I run my test code, my error states... ' "Member" object does not support indexing'. My main problems that if a player were to have the same surname then both would be deleted, so my only way round this is to have a unique i.d. for each player... Unless there's a way to have two search criteria's for deleting a player, i.e. forename and surname.

    Example data from players.txt is...
    A;Ed;Young;e.yo ung@lboro.ac.uk ;223376;1;0;1;0
    B;Gordon;McTagg art-Cowan;g.b.mctag gart@lboro.ac.u k;226276;1;0;1; 0
    C;Tom;Kane;t.g. kane@lboro.ac.u k;726653;1;0;1; 0
    D;Chris;Holmes; c.r.holmes@lbor o.ac.uk;225378; 1;0;1;0
    E;David;Mulvane y;d.k.mulvaney@ lboro.ac.uk;227 142;1;0;1;0
    F;James;Buller; j.buller@lboro. ac.uk;223543;1; 0;1;0

    I hope your able to understand what I'm trying to get at and can shed some ideas on how I'd be able to do this.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    There seem to be a couple of problems. Anything's possible - it just depends on what you want.

    Anyway, the first problem is that you actually have two different structures for members. In the first part of the code you go through players.txt and create objects of the class Member which you add to the list. In the new_member function you add a list of member attributes.

    It doesn't make sense to do it this way, because everything else would then need to be able to handle both structures. You delete function seems to be aimed at the latter (the list of attributes), and your Member class is throwing an error because you can't index on it.

    I hope this helps. Let us know how you actually want to do it, and we can go from there!

    Comment

    • Derbylad
      New Member
      • May 2010
      • 7

      #3
      Originally posted by Glenton
      Hi

      There seem to be a couple of problems. Anything's possible - it just depends on what you want.

      Anyway, the first problem is that you actually have two different structures for members. In the first part of the code you go through players.txt and create objects of the class Member which you add to the list. In the new_member function you add a list of member attributes.

      It doesn't make sense to do it this way, because everything else would then need to be able to handle both structures. You delete function seems to be aimed at the latter (the list of attributes), and your Member class is throwing an error because you can't index on it.

      I hope this helps. Let us know how you actually want to do it, and we can go from there!
      Thanks for the reply.

      Ideally in the "new_player function" I want to add a set of objects to the class and move away from the list of attributes. We've only just touched on classes and objects so I'm still pretty new to it.

      The same goes for the delete function. Ideally I want the function to be able to input a specific object i.e. (First Name + Surname or a Unique I.D), and then delete all objects relating to it for that member, therefore deleting them from the class.

      Comment

      • woooee
        New Member
        • Mar 2008
        • 43

        #4
        l is a list of pointers to individual instances of the memb (Member) class, so you should look at the forename that each instance points to, and check what you are doing at the same time. Please don't use "i", "l", " or "O" as single letter variable names because they look too much like numbers. Untested code =
        Code:
        def delete_player(forename):
            """
            Delete a member from the list
                forename = a players forename (str)
            """
            for memb_class in l:
                print("checking names", memb_class.forename, forename)
                ## a print statement would have shown the problem with
                ## if x[1] == forename:
        
                ## if statement can be anything you want, like
                ##  if (memb_class.forename == forename) and \
                ##     (memb_class.surname == surname):
                if memb_class.forename == forename:
                    l.remove(memb_class)
                    break
        #
        #     or
            for ctr, memb_class in enumerate(l):
                print("checking names", memb_class.forename, forename)
                if memb_class.forename == forename:
                    del l[ctr]
                    break
        
            ## check that it was deleted
            print("\n 'l' now =")
            for memb_class in l:
                print memb_class.forename

        Comment

        • Derbylad
          New Member
          • May 2010
          • 7

          #5
          Originally posted by woooee
          l is a list of pointers to individual instances of the memb (Member) class, so you should look at the forename that each instance points to, and check what you are doing at the same time. Please don't use "i", "l", " or "O" as single letter variable names because they look too much like numbers. Untested code =
          Code:
          def delete_player(forename):
              """
              Delete a member from the list
                  forename = a players forename (str)
              """
              for memb_class in l:
                  print("checking names", memb_class.forename, forename)
                  ## a print statement would have shown the problem with
                  ## if x[1] == forename:
          
                  ## if statement can be anything you want, like
                  ##  if (memb_class.forename == forename) and \
                  ##     (memb_class.surname == surname):
                  if memb_class.forename == forename:
                      l.remove(memb_class)
                      break
          #
          #     or
              for ctr, memb_class in enumerate(l):
                  print("checking names", memb_class.forename, forename)
                  if memb_class.forename == forename:
                      del l[ctr]
                      break
          
              ## check that it was deleted
              print("\n 'l' now =")
              for memb_class in l:
                  print memb_class.forename
          Thank you again for a hasty response.

          I've just tested the first code and this works perfectly.
          I was unsure about the second method though...
          Code:
          #
          #     or
              for ctr, memb_class in enumerate(l):
                  print("checking names", memb_class.forename, forename)
                  if memb_class.forename == forename:
                      del l[ctr]
                      break
          If you could just explain how this works. It's the first time I've come across "enumerate" and was there a specific reason for using 'ctr'.

          Is it also possible to add two search criteria (i.e. (memb_class.for ename == forename and memb_class.surn ame ==surname) to negate the chances of deleting people with the same forename.

          Out of interest which would be the preferable method for a professional programmer?

          Thanks for your time and i'll avoid using "i", "l", " or "O" as variable names in the future.

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Hi

            I haven't checked it in full, but I can't help feeling the second method is a bit suspect.

            Code:
                for ctr, memb_class in enumerate(l):
                    print("checking names", memb_class.forename, forename)
                    if memb_class.forename == forename:
                        del l[ctr]
                        break
            For example, if one element were to be deleted, it could knock out the indexing, so that the next time the element after the one you want to delete is removed. But the rest looks good. Obviously you've got to re-write new_player.

            The other thing you could do is define some special class methods. Eg __repr__ can be used so that you can print a nice readable version of your member class instance.

            But more relevant to you might be the __eq__ method. This allows you to use the == operator in your code. E.g. l[0]=="Jones", will return True if the surname is "Jones" and False otherwise. It's a neat way of packing all the ugliness away into something more human readable!

            Comment

            Working...