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."
7 1281
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: -
def RemoveName(self):
-
lastname = raw_input("Enter the last name of the person you want to remove ")
-
# Search a list containing full names
-
for i, full_name in enumerate(list_1):
-
if lastname in full_name:
-
list_1.remove(i)
-
print "Removed", i.name, i.lastname
-
return
-
else:
-
print "The name doesn't exist."
-
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. - 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."
Try something like this: - name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
-
for i in range(len(name_list)):
-
if "Smith" in name_list[i]:
-
del name_list[i]
-
break
-
print name_list
-
['Bob Jones', 'Bill Sanders']
or this: - for i in range(len(name_list)):
-
if name_list[i].split()[1] == "Smith":
-
del name_list[i]
-
break
or this: - import string
-
name_to_remove = "SMITH"
-
for i in range(len(name_list)):
-
if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
-
del name_list[i]
-
break
one more: - >>> import string
-
>>> name_to_remove = "SMITH"
-
>>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
-
>>> for s in name_list:
-
... if string.lower(name_to_remove) in string.lower(s):
-
... del name_list[name_list.index(s)]
-
...
-
>>> print name_list
-
['Bob Jones', 'Bill Sanders']
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: -
name = "John Smith"
-
print name.lower()
Try something like this: - name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
-
for i in range(len(name_list)):
-
if "Smith" in name_list[i]:
-
del name_list[i]
-
break
-
print name_list
-
['Bob Jones', 'Bill Sanders']
or this: - for i in range(len(name_list)):
-
if name_list[i].split()[1] == "Smith":
-
del name_list[i]
-
break
or this: - import string
-
name_to_remove = "SMITH"
-
for i in range(len(name_list)):
-
if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
-
del name_list[i]
-
break
one more: - >>> import string
-
>>> name_to_remove = "SMITH"
-
>>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
-
>>> for s in name_list:
-
... if string.lower(name_to_remove) in string.lower(s):
-
... del name_list[name_list.index(s)]
-
...
-
>>> print name_list
-
['Bob Jones', 'Bill Sanders']
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: -
name = "John Smith"
-
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.
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. - class Person:
-
def __init__(self, firstname, lastname, birthday, adress):
-
self.firstname = firstname
-
self.lastname = lastname
-
self.birthday = birthday
-
self.adress = adress
-
-
-
-
class Register:
-
def readFromFile(self):
-
global name_list
-
name_list = list()
-
name_file = open("names.txt", "r")
-
line = name_file.readline()
-
while line != "":
-
firstname = name_file.readline()
-
lastname = name_file.readline()
-
birthday = name_file.readline()
-
adress = name_file.readline()
-
name_list.append(Person(firstname, lastname, birthday, adress))
-
line = name_file.readline()
-
name_file.close()
-
-
-
def Remove(self):
-
name = raw_input("Enter the last name of the person you want to delete ")
-
for i in name_list:
-
if name == i.lastname:
-
name_list.remove(i)
-
print "Removed", i.firstname, i.lastname
-
return
-
else:
-
print "The name doesn't exist."
That helps a lot! Here is something to get you started: - class Person:
-
def __init__(self, firstname, lastname, birthday, adress):
-
self.firstname = firstname
-
self.lastname = lastname
-
self.birthday = birthday
-
self.adress = adress
-
-
-
-
class Register:
-
def readFromFile(self):
-
global name_list
-
name_list = list()
-
name_file = open("names.txt", "r")
-
line = name_file.readline()
-
while line != "":
-
firstname = name_file.readline()
-
lastname = name_file.readline()
-
birthday = name_file.readline()
-
adress = name_file.readline()
-
name_list.append(Person(firstname, lastname, birthday, adress))
-
line = name_file.readline()
-
name_file.close()
-
-
-
def Remove(self):
-
name = raw_input("Enter the last name of the person you want to delete ")
-
for person in name_list:
-
if name == person.lastname:
-
name_list.remove(person)
-
print "Removed", person.firstname, person.lastname
-
return
-
else:
-
print "The name doesn't exist."
-
-
-
if __name__ == "__main__": # standard way to test a module
-
name_list = []
-
p = Person('joe', 'blow', 'june 12 1972', '1234 anywhere')
-
name_list.append(p)
-
p = Person('john', 'doe', 'june 12 1972', '1234 anywhere')
-
name_list.append(p)
-
-
print name_list
-
-
reg = Register()
-
reg.Remove()
-
-
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. - class Person:
-
def __init__(self, firstname, lastname, birthday, adress):
-
self.firstname = firstname
-
self.lastname = lastname
-
self.birthday = birthday
-
self.adress = adress
-
-
-
-
class Register:
-
def readFromFile(self):
-
global name_list
-
name_list = list()
-
name_file = open("names.txt", "r")
-
line = name_file.readline()
-
while line != "":
-
firstname = name_file.readline()
-
lastname = name_file.readline()
-
birthday = name_file.readline()
-
adress = name_file.readline()
-
name_list.append(Person(firstname, lastname, birthday, adress))
-
line = name_file.readline()
-
name_file.close()
-
-
-
def Remove(self):
-
name = raw_input("Enter the last name of the person you want to delete ")
-
for i in name_list:
-
if name == i.lastname:
-
name_list.remove(i)
-
print "Removed", i.firstname, i.lastname
-
return
-
else:
-
print "The name doesn't exist."
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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:...
|
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,...
|
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...
|
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...
|
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...
|
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....
|
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 ...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| | |