
-----------------------------------
G-nerd
Thu Nov 04, 2010 5:33 pm

How to delete elements in python using a del function
-----------------------------------
can you tell me how to write a function, called bawdlerize(, ), which removes an unsavory word,
, from a sentence, . Use the "del" operator on lists for this function. 

Example:

s = 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

   

-----------------------------------
DtY
Thu Nov 04, 2010 5:55 pm

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:
word in sent
If it is, you can remove the first occurrence of it with:
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.

-----------------------------------
jcollins1991
Thu Nov 04, 2010 6:07 pm

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.

-----------------------------------
G-nerd
Thu Nov 04, 2010 6:26 pm

Re: 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:
word in sent
If it is, you can remove the first occurrence of it with:
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

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.

-----------------------------------
rdrake
Thu Nov 04, 2010 7:24 pm

Re: How to delete elements in python using a del function
-----------------------------------
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 sdel L
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.

-----------------------------------
G-nerd
Thu Nov 04, 2010 7:47 pm

Re: How to delete elements in python using a del function
-----------------------------------
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 sdel L
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.





import string



def bawdlerize(s,w):
    for i in range(len(s)):
        if w == s

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.

-----------------------------------
rdrake
Thu Nov 04, 2010 8:28 pm

Re: How to delete elements in python using a del function
-----------------------------------

def bawdlerize(s,w):
    for i in range(len(s)):
        if w == sDisregard 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.

>>> def bawdlerize(s, w):
...     while w in s:
...             del s
