By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

I need some help, please

P: 5
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
Share this Question
Share on Google+
7 Replies


bartonc
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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

P: 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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

Post your reply

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