473,387 Members | 1,549 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,387 software developers and data experts.

I need some help, please

Hi

I'm trying remove a person from a list but it's not working.
How can I make it work, please help me.

def RemoveName(self):
lastname = raw_input("Enter the last name of the person you want to remove ")
for i in list_1:
if lastname == i.lastname:
list_1.remove(i)
print "Removed", i.name, i.lastname
return
else:
print "The name doesn't exist."
Nov 24 '06 #1
7 1281
bartonc
6,596 Expert 4TB
I didn't have time to test this, but it shows you some good tricks.
In your next post, please USE CODE TAGS as described in "posting guidelines", found in the panel on your right when you are posting or in the sticky thread (at the top of this forum). Your post should look like this:

Expand|Select|Wrap|Line Numbers
  1. def RemoveName(self):
  2.     lastname = raw_input("Enter the last name of the person you want to remove ")
  3.     # Search a list containing full names
  4.     for i, full_name in enumerate(list_1):
  5.         if lastname in full_name:
  6.             list_1.remove(i)
  7.             print "Removed", i.name, i.lastname
  8.             return
  9.         else:
  10.             print "The name doesn't exist."
  11.  
Nov 24 '06 #2
bvdet
2,851 Expert Mod 2GB
Hi

I'm trying remove a person from a list but it's not working.
How can I make it work, please help me.

Expand|Select|Wrap|Line Numbers
  1. def RemoveName(self):
  2.     lastname = raw_input("Enter the last name of the person you want to remove ")
  3.     for i in list_1:
  4.         if lastname == i.lastname:
  5.             list_1.remove(i)
  6.             print "Removed", i.name, i.lastname
  7.             return
  8.         else:
  9.             print "The name doesn't exist."
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
  2. for i in range(len(name_list)):
  3.     if "Smith" in name_list[i]:
  4.            del name_list[i]
  5.         break
  6. print name_list
  7. ['Bob Jones', 'Bill Sanders']
or this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(name_list)):
  2.     if name_list[i].split()[1] == "Smith":
  3.            del name_list[i]
  4.         break
or this:
Expand|Select|Wrap|Line Numbers
  1. import string
  2. name_to_remove = "SMITH"
  3. for i in range(len(name_list)):
  4.     if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
  5.         del name_list[i]
  6.         break
one more:
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> name_to_remove = "SMITH"
  3. >>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
  4. >>> for s in name_list:
  5. ...     if string.lower(name_to_remove) in string.lower(s):
  6. ...         del name_list[name_list.index(s)]
  7. ...         
  8. >>> print name_list
  9. ['Bob Jones', 'Bill Sanders']
Nov 24 '06 #3
bartonc
6,596 Expert 4TB
But don't import string to use these methods!!! This module has been deprecated (meaning DON'T USE THIS BECAUSE IT MAY NOT ALWAYS BE AVAILABLE). Instead, use the string itself as the object whose method you call:
Expand|Select|Wrap|Line Numbers
  1. name = "John Smith"
  2. print name.lower()

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
  2. for i in range(len(name_list)):
  3.     if "Smith" in name_list[i]:
  4.            del name_list[i]
  5.         break
  6. print name_list
  7. ['Bob Jones', 'Bill Sanders']
or this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(name_list)):
  2.     if name_list[i].split()[1] == "Smith":
  3.            del name_list[i]
  4.         break
or this:
Expand|Select|Wrap|Line Numbers
  1. import string
  2. name_to_remove = "SMITH"
  3. for i in range(len(name_list)):
  4.     if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
  5.         del name_list[i]
  6.         break
one more:
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> name_to_remove = "SMITH"
  3. >>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
  4. >>> for s in name_list:
  5. ...     if string.lower(name_to_remove) in string.lower(s):
  6. ...         del name_list[name_list.index(s)]
  7. ...         
  8. >>> print name_list
  9. ['Bob Jones', 'Bill Sanders']
Nov 26 '06 #4
bvdet
2,851 Expert Mod 2GB
But don't import string to use these methods!!! This module has been deprecated (meaning DON'T USE THIS BECAUSE IT MAY NOT ALWAYS BE AVAILABLE). Instead, use the string itself as the object whose method you call:
Expand|Select|Wrap|Line Numbers
  1. name = "John Smith"
  2. print name.lower()
I got the message Barton. My Python Essential Reference is based on 1.5.2, and my applications execute in 2.3, so I may be out of date. I'll try to keep up.
Nov 26 '06 #5
hi

thanks for the answers but what I'm trying do do is that I have a file with the first name, last name, birth day, adress and phonenumber written on seperate lines. Then I have read the information into a list. and then I want user to be able to type in the last name of the person they want to delete and then the person with all the information about birth day, adress and phone number should also be deleted from the list and from the file.

This is how the whole program looks like.
How can I make the method Remove work. It only says the name dosen't exist. I have tried to make this work for a long time now. Please help me.

Expand|Select|Wrap|Line Numbers
  1.  class Person:
  2.     def __init__(self, firstname, lastname, birthday, adress):
  3.         self.firstname = firstname
  4.         self.lastname = lastname
  5.         self.birthday = birthday
  6.         self.adress = adress
  7.  
  8.  
  9.  
  10. class Register:
  11.     def readFromFile(self):
  12.         global name_list
  13.         name_list = list()
  14.         name_file = open("names.txt", "r")
  15.         line = name_file.readline()
  16.         while line != "":
  17.             firstname = name_file.readline()
  18.             lastname = name_file.readline()
  19.             birthday = name_file.readline()
  20.             adress = name_file.readline()
  21.             name_list.append(Person(firstname, lastname, birthday, adress))
  22.             line = name_file.readline()
  23.         name_file.close()
  24.  
  25.  
  26.     def Remove(self):
  27.         name = raw_input("Enter the last name of the person you want to delete ")
  28.         for i in name_list:
  29.             if name == i.lastname:
  30.                 name_list.remove(i)
  31.                 print "Removed", i.firstname, i.lastname
  32.                 return
  33.             else:
  34.                 print "The name doesn't exist." 
Nov 26 '06 #6
bartonc
6,596 Expert 4TB
That helps a lot! Here is something to get you started:

Expand|Select|Wrap|Line Numbers
  1. class Person:
  2.     def __init__(self, firstname, lastname, birthday, adress):
  3.         self.firstname = firstname
  4.         self.lastname = lastname
  5.         self.birthday = birthday
  6.         self.adress = adress
  7.  
  8.  
  9.  
  10. class Register:
  11.     def readFromFile(self):
  12.         global name_list
  13.         name_list = list()
  14.         name_file = open("names.txt", "r")
  15.         line = name_file.readline()
  16.         while line != "":
  17.             firstname = name_file.readline()
  18.             lastname = name_file.readline()
  19.             birthday = name_file.readline()
  20.             adress = name_file.readline()
  21.             name_list.append(Person(firstname, lastname, birthday, adress))
  22.             line = name_file.readline()
  23.         name_file.close()
  24.  
  25.  
  26.     def Remove(self):
  27.         name = raw_input("Enter the last name of the person you want to delete ")
  28.         for person in name_list:
  29.             if name == person.lastname:
  30.                 name_list.remove(person)
  31.                 print "Removed", person.firstname, person.lastname
  32.                 return
  33.         else:
  34.             print "The name doesn't exist."
  35.  
  36.  
  37. if __name__ == "__main__":  # standard way to test a module
  38.     name_list = []
  39.     p = Person('joe', 'blow', 'june 12 1972', '1234 anywhere')
  40.     name_list.append(p)
  41.     p = Person('john', 'doe', 'june 12 1972', '1234 anywhere')
  42.     name_list.append(p)
  43.  
  44.     print name_list
  45.  
  46.     reg = Register()
  47.     reg.Remove()
  48.  
  49.     print name_list
hi

thanks for the answers but what I'm trying do do is that I have a file with the first name, last name, birth day, adress and phonenumber written on seperate lines. Then I have read the information into a list. and then I want user to be able to type in the last name of the person they want to delete and then the person with all the information about birth day, adress and phone number should also be deleted from the list and from the file.

This is how the whole program looks like.
How can I make the method Remove work. It only says the name dosen't exist. I have tried to make this work for a long time now. Please help me.

Expand|Select|Wrap|Line Numbers
  1.  class Person:
  2.     def __init__(self, firstname, lastname, birthday, adress):
  3.         self.firstname = firstname
  4.         self.lastname = lastname
  5.         self.birthday = birthday
  6.         self.adress = adress
  7.  
  8.  
  9.  
  10. class Register:
  11.     def readFromFile(self):
  12.         global name_list
  13.         name_list = list()
  14.         name_file = open("names.txt", "r")
  15.         line = name_file.readline()
  16.         while line != "":
  17.             firstname = name_file.readline()
  18.             lastname = name_file.readline()
  19.             birthday = name_file.readline()
  20.             adress = name_file.readline()
  21.             name_list.append(Person(firstname, lastname, birthday, adress))
  22.             line = name_file.readline()
  23.         name_file.close()
  24.  
  25.  
  26.     def Remove(self):
  27.         name = raw_input("Enter the last name of the person you want to delete ")
  28.         for i in name_list:
  29.             if name == i.lastname:
  30.                 name_list.remove(i)
  31.                 print "Removed", i.firstname, i.lastname
  32.                 return
  33.             else:
  34.                 print "The name doesn't exist." 
Nov 26 '06 #7
bartonc
6,596 Expert 4TB
I got the message Barton. My Python Essential Reference is based on 1.5.2, and my applications execute in 2.3, so I may be out of date. I'll try to keep up.
Sorry, BV. I missed this post and have been aiming my posts at getting your attention.
Nov 28 '06 #8

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

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
8
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.