473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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...".

Expand|Select|Wrap|Line Numbers
  1. class Member(object):
  2.     '''a member in a squash team'''
  3.  
  4. players = open ("players.txt","r")
  5.  
  6. l= []
  7. for line in players:
  8.     line=line.strip()
  9.     x = line.split(";")
  10.     memb = Member()
  11.     memb.id= x[0]
  12.     memb.forename= x[1]
  13.     memb.surname= x[2]
  14.     memb.email= x[3]
  15.     memb.phone= x[4]
  16.     memb.divc= x[5]
  17.     memb.pointc= x[6]
  18.     memb.divp= x[7]
  19.     memb.pointp= x[8]
  20.     l.append(memb)
  21. players. close ()
  22.  
  23. def new_player(member_id,forename,surname,email,phone_number,division_current,points_current,division_previous,points_previous):
  24.     """
  25.     Adds a new member to the register.
  26.         member_id = a number representing the 
  27.         forename = a players forename
  28.         surname = a players surname
  29.         email = a players email
  30.         phone_number = a players phone number
  31.         division_current = the current division the player is in
  32.         points_current = the current points the player has
  33.         division_previous = the previous division the player was in
  34.         points_previous = the amount of points the player attained in the last round
  35.     """
  36.     l.append([member_id,forename,surname,email,phone_number,division_current,points_current,division_previous,points_previous])
  37.  
  38. def delete_player(forename):
  39.     """
  40.     Delete a member from the list
  41.         forename = a players forename (str)
  42.     """
  43.     for j in range(0,len(l)):
  44.         x = l[j]
  45.         if x[1] == forename:
  46.             del l[j]
  47.             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.young@lboro.ac.uk;223376;1;0;1;0
B;Gordon;McTaggart-Cowan;g.b.mctaggart@lboro.ac.uk;226276;1;0;1;0
C;Tom;Kane;t.g.kane@lboro.ac.uk;726653;1;0;1;0
D;Chris;Holmes;c.r.holmes@lboro.ac.uk;225378;1;0;1 ;0
E;David;Mulvaney;d.k.mulvaney@lboro.ac.uk;227142;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.
May 9 '10 #1
5 3394
Glenton
391 Expert 256MB
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!
May 10 '10 #2
@Glenton
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.
May 10 '10 #3
woooee
43
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 =
Expand|Select|Wrap|Line Numbers
  1. def delete_player(forename):
  2.     """
  3.     Delete a member from the list
  4.         forename = a players forename (str)
  5.     """
  6.     for memb_class in l:
  7.         print("checking names", memb_class.forename, forename)
  8.         ## a print statement would have shown the problem with
  9.         ## if x[1] == forename:
  10.  
  11.         ## if statement can be anything you want, like
  12.         ##  if (memb_class.forename == forename) and \
  13.         ##     (memb_class.surname == surname):
  14.         if memb_class.forename == forename:
  15.             l.remove(memb_class)
  16.             break
  17. #
  18. #     or
  19.     for ctr, memb_class in enumerate(l):
  20.         print("checking names", memb_class.forename, forename)
  21.         if memb_class.forename == forename:
  22.             del l[ctr]
  23.             break
  24.  
  25.     ## check that it was deleted
  26.     print("\n 'l' now =")
  27.     for memb_class in l:
  28.         print memb_class.forename 
May 10 '10 #4
@woooee
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...
Expand|Select|Wrap|Line Numbers
  1. #
  2. #     or
  3.     for ctr, memb_class in enumerate(l):
  4.         print("checking names", memb_class.forename, forename)
  5.         if memb_class.forename == forename:
  6.             del l[ctr]
  7.             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.forename == forename and memb_class.surname ==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.
May 10 '10 #5
Glenton
391 Expert 256MB
Hi

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

Expand|Select|Wrap|Line Numbers
  1.     for ctr, memb_class in enumerate(l):
  2.         print("checking names", memb_class.forename, forename)
  3.         if memb_class.forename == forename:
  4.             del l[ctr]
  5.             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!
May 11 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ckroom | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Could anybody tell me if this code is right? Thanks. struct lista_alumnos {
3
by: Crimarc | last post by:
A list with un-sorted members Along with a sub-list of that list with also un-sorted members Could you tell me how to delete the sub-list members present in the given list without first sorting...
10
by: Daniel Vukadinovic | last post by:
OK, here's the deal. I'd like to delete the list.Here's what I do: node* p_temp = p_beginning; while (p_beginning != 0) { p_beginning = p_beginning->p_next; delete p_beginning; }
4
by: Alex Buell | last post by:
Hi, I have quite a lot of strings that needs to go into a vector. Rather than doing this: std::vector<std::string> list; // not to be confused with std::list list.push_back("a string");...
10
by: Boltar | last post by:
Hello I need to iterate through an STL list container and delete certain entries in it. I realise if use erase() with the iterator it will invalidate it but what if I store it beforehand? Ie: is...
2
by: Fenris54 | last post by:
Basically, I have a listbox that is holding some information stored in a database. I want to remove the items but I'm not quit sure how. I've gotten the impression that I need to store the selected...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the...
31
by: Markus Pitha | last post by:
Hello, I'm using a template to simulate a LinkedList from Java.It works without problems, but when I want to use strings in main.cpp instead of char*, I get the following error message: $...
1
by: xian83 | last post by:
I am trying all day to make some things straight but nothing helps... Me and my collegue have built a c++ tree of a class VO. I tend to refer to the structure as a tree and not a simple list...
6
by: mbk006 | last post by:
I am new for linked lists. So please help. So any one kindly send the algorithms for creating single linked list, sorting linked list and Deleting link list
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.