How to delete elements in python using a del function
Author |
Message |
G-nerd
|
Posted: Thu Nov 04, 2010 5:33 pm Post subject: How to delete elements in python using a del function |
|
|
can you tell me how to write a function, called bawdlerize(<sent>, <word>), which removes an unsavory word,
<word>, from a sentence, <sent>. Use the "del" operator on lists for this function.
Example:
s = ["now", "is", "the", "time", "for", "all", "good", "men", "to",
"come", "to", "the", "aid", "of", "their", "parties"]
bawdlerize(s, "to")
s = ["now", "is", "the", "time", "for", "all", "good", "men", "come",
"the", "aid", "of", "their", "parties"]
When I do run the program it just prints the string inputted into a list. From what I am reading it should delete w because I said for all the elements in s( which is a list of strings), if i is not present in the list, delete w. Then I put return s so that if returns the value with the deleted word.
What am I doing wrong. Any hint would be much appreciated.
import string
Quote: sentence=raw_input("Enter a string")
s= sentence.split()
def bowdlerize(s,w):
for i in range(len(s)):
if i not in range(len(s)):
del w
return s
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Thu Nov 04, 2010 5:55 pm Post subject: RE:How to delete elements in python using a del function |
|
|
Is this a school assignment?
Off the top of my head I can think of two better ways of doing this than using del.
- You can find out if the word still exists in the list using:
If it is, you can remove the first occurrence of it with:
Python: | sent.remove(word) |
Just keep doing that until word is not in sent.
- You can use the filter() function to get rid of them all at once. The function you give to filter would return true if the word you give it is not the word it is removing
http://docs.python.org/library/functions.html#filter
The problem with using del is that it removes by index, and not value, but when you remove one of them, all the indices after it change.
e; The specific problem with your code is that "del w" deletes the variable w, what you want to do is "del sent[w]" to delete the wth index of sent. But, like I said before, that will change the indices and the length of the list, so you'll be having more problems. |
|
|
|
|
![](images/spacer.gif) |
jcollins1991
|
Posted: Thu Nov 04, 2010 6:07 pm Post subject: Re: How to delete elements in python using a del function |
|
|
Since you're deleting stuff from the list you'd eventually get out of index errors. IMO the two easiest options would be to build a new list, or instead of splitting a string and doing array operations you could just use a regular expression function like re.sub("\s?%s\s?" % filter_word, " ", input), which removes any word and any surrounding whitespace, then substitutes in a single space. |
|
|
|
|
![](images/spacer.gif) |
G-nerd
|
Posted: Thu Nov 04, 2010 6:26 pm Post subject: Re: RE:How to delete elements in python using a del function |
|
|
DtY @ Thu Nov 04, 2010 5:55 pm wrote: Is this a school assignment?
Off the top of my head I can think of two better ways of doing this than using del.
- You can find out if the word still exists in the list using:
If it is, you can remove the first occurrence of it with:
Python: | sent.remove(word) |
Just keep doing that until word is not in sent.
- You can use the filter() function to get rid of them all at once. The function you give to filter would return true if the word you give it is not the word it is removing
http://docs.python.org/library/functions.html#filter
The problem with using del is that it removes by index, and not value, but when you remove one of them, all the indices after it change.
e; The specific problem with your code is that "del w" deletes the variable w, what you want to do is "del sent[w]" to delete the wth index of sent. But, like I said before, that will change the indices and the length of the list, so you'll be having more problems.
No I need to know how to use the del function for my upcoming midterm. I am expected to know how to delete elements in lists using the del fuction. Also even though I change the del w into del s[w], it still produces the list of strings without the deleted designated element. |
|
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Thu Nov 04, 2010 7:24 pm Post subject: Re: How to delete elements in python using a del function |
|
|
G-nerd @ Thu Nov 04, 2010 5:33 pm wrote: Python: | def bowdlerize(s,w):
for i in range(len(s)):
if i not in range(len(s)):
del w
return s
|
False. Your first two and last lines are correct, but the middle is wrong. Instead of doing if i not in... you need to check to see if the value of s[i] is equal to the word you're looking for. If it is, delete it.
The syntax is as follows:
Where L is a list and i is the index the word you wish to delete is at.
You can also do it with a while loop, but I'm sure the above solution will fit your needs just fine. |
|
|
|
|
![](images/spacer.gif) |
G-nerd
|
Posted: Thu Nov 04, 2010 7:47 pm Post subject: Re: How to delete elements in python using a del function |
|
|
rdrake @ Thu Nov 04, 2010 7:24 pm wrote: G-nerd @ Thu Nov 04, 2010 5:33 pm wrote: Python: | def bowdlerize(s,w):
for i in range(len(s)):
if i not in range(len(s)):
del w
return s
|
False. Your first two and last lines are correct, but the middle is wrong. Instead of doing if i not in... you need to check to see if the value of s[i] is equal to the word you're looking for. If it is, delete it.
The syntax is as follows:
Where L is a list and i is the index the word you wish to delete is at.
You can also do it with a while loop, but I'm sure the above solution will fit your needs just fine.
Quote:
import string
def bawdlerize(s,w):
for i in range(len(s)):
if w == s[i]:
del s[i]
return s
sentence=raw_input("Enter a string:")
s= sentence.split()
I changed my code to this to make it delete the ith element of the list. However for some reason it just prints out the lsit of strings without the designated deleted string. I am for sure that it should delete the element I desire. |
|
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Thu Nov 04, 2010 8:28 pm Post subject: Re: How to delete elements in python using a del function |
|
|
G-nerd @ Thu Nov 04, 2010 7:47 pm wrote: Python: |
def bawdlerize(s,w):
for i in range(len(s)):
if w == s[i]:
del s[i]
return s
sentence=raw_input("Enter a string:")
s= sentence.split() |
Disregard my earlier advice. As stated above this will lead to index-out-of-range issues. While the delete statement is correct, you could end up trying to look at list elements that no longer exist.
We usually don't give out solutions, but you seem to have a solution already anyway. This one's just better.
Python: | >>> def bawdlerize(s, w):
... while w in s:
... del s[s.index(w)]
... return s
...
>>> print(bawdlerize(['lol', 'no', 'u'], 'no'))
['lol', 'u']
>>> print(bawdlerize(['lol', 'no', 'no', 'u'], 'no'))
['lol', 'u'] |
|
|
|
|
|
![](images/spacer.gif) |
|
|