7

Possible Duplicate:
How to find positions of the list maximum?

A question from homework: Define a function censor(words,nasty) that takes a list of words, and replaces all the words appearing in nasty with the word CENSORED, and returns the censored list of words.

>>> censor([’it’,’is’,’raining’], [’raining’])
[’it’,’is’,’CENSORED’]

I see solution like this:

  1. find an index of nasty
  2. replace words matching that index with "CENSORED"

but i get stuck on finding the index..

Community
  • 1
  • 1
Gusto
  • 1,483
  • 9
  • 23
  • 34
  • @mart: OP says it's homework. – SilentGhost Nov 04 '10 at 17:56
  • @Silent: OK, sorry if it was voluntary. See the link in my comment about meta tags attached to @z4y4ts's answer. – martineau Nov 04 '10 at 18:37
  • @martineau: well, homework is not in that list and it does seem alive and kicking. I should also note that I deeply despise those misguided attempts in social engineering originating from our corporate overlords. – SilentGhost Nov 04 '10 at 19:18
  • 1
    @SilentGhost: Did you read the part where @Aaronut wrote "The reason meta-tags are a problem is that they do not describe the content of the question"? which is the crux of why they're looked upon poorly. How many regular users do you think ever search for the tag "homework" -- so what good is it? – martineau Nov 04 '10 at 20:26
  • @Silent: [homework] *is* in that list: "the homework tag, like other so-called "meta" tags, is now discouraged." http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions/10812#10812 –  Nov 06 '10 at 12:02
  • @Roger: is it not just a personal opinion of Joel's? I don't see how this question is any different from 5276 others homework questions. – SilentGhost Nov 06 '10 at 13:30
  • @Silent: It is not just Joel's opinion: [ **"Meta-tagging is explicitly discouraged."** ](http://meta.stackexchange.com/q/60015) This is site *policy.* See the two criteria given: 1) can [homework] work as the only tag on the question? no. 2) does it mean different things to different people? yes, it isn't uncommon for people to retag with [homework] to mean "basics" or "beginner". –  Nov 06 '10 at 13:43
  • @Roger: so then it clearly doesn't apply to this question, does it? it's not the only tag on a the question, and the reason I put it there is the OP opening: *A question from homework: *. I think the meta tags are extremely useful, take OP for example 12 question, 0 answers, 7 homework tags, probably would have about a dozen of beginner tag too. I'd prefer to have this information not to waste too much time. – SilentGhost Nov 06 '10 at 14:23
  • @Silent: Re-read the criteria listed in the blog post; you misunderstand how to apply it. If you don't want to answer this question, then *don't answer it.* But also don't try to brand the question or the poster with your own subjective viewpoint by tagging as [homework] or [beginner]. –  Nov 06 '10 at 14:26
  • @Roger: **I don't brand anyone with anything, OP said it was a homework question and so I tagged the question accordingly**. Maybe "official policies" would be more useful and respected, if the there wasn't so many idiotic of them around and their defence wouldn't deteriorate into civil war. – SilentGhost Nov 06 '10 at 14:30
  • @Silent: If the OP said "I want to start a subjective discussion...", would you tag as [subjective] and [discussion]? If someone says "I'm writing my PhD thesis about X...", would you tag as [homework] or [X]? –  Nov 06 '10 at 14:32
  • @Roger: I did vote to close this question if that's what you're implying. – SilentGhost Nov 06 '10 at 14:34
  • No, that is not what I'm saying at all. I'm saying picking out a particular word from the post is not enough to tag with that word. –  Nov 06 '10 at 14:35
  • @Silent: When someone flat out says "this isn't homework", [why do you tag it as homework?](http://stackoverflow.com/q/4099042/54262) Apparently it's *not* the content of the question that matters for you to tag [homework], and that shows use as a meta-tag. –  Nov 06 '10 at 14:37
  • @Roger: are you kidding me? how's exam preparation is not a homework? and I have already said I see meta tags as a useful tool in the community. – SilentGhost Nov 06 '10 at 15:00
  • @Silent: Because it's not. You seem to want to tag anything done by a student as homework, regardless of what is actually asked or whether it was actually assigned by a teacher. Why not include interview preparation and questions as [homework]? It's certainly work done at home with the goal of self-improvement rather than solving a "real problem." –  Nov 06 '10 at 15:26

4 Answers4

39

You can find the index of any element of a list by using the .index method.

>>> l=['a','b','c']
>>> l.index('b')
1
MAK
  • 26,140
  • 11
  • 55
  • 86
  • 3
    Unless there are two cases of 'b' in the list, in which case you will only get the first one. That could be a problem with this question. – philosodad Nov 04 '10 at 17:15
  • 2
    Can't believe I had to scroll all the way down here to find this.... – Menasheh Jul 16 '17 at 21:13
1

Your approach might work, but it’s unnecessarily complicated.

Python allows a very simple syntax to check whether something is contained in a list:

censor = [ 'bugger', 'nickle' ]
word = 'bugger'
if word in censor: print 'CENSORED'

With that approach, simply walk over your list of words and test for each words whether it’s in the censor list.

To walk over your list of words, you can use the for loop. Since you might need to modify the current word, use an index, like so:

for index in len(words)):
   print index, words[index]

Now all you need to do is put the two code fragments together.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • ... and then score some brownie points by turning the above O(#words * #nastywords) algorithm into a O(#words). :) – janneb Nov 04 '10 at 14:07
  • @janneb: Sure this works? All I can achieve is expected (!) time O(#words + #censor) – by converting the censored words list to a `set`. – Konrad Rudolph Nov 04 '10 at 14:11
  • Well, yes, you're right. My point was that one can, by the use of a set as you point out (or dict for older python versions), turn this quadratic algo into a linear one. – janneb Nov 04 '10 at 14:33
1

Actually you don't have to operate with indexes here. Just iterate over words list and check if the word is listed in nasty. If it is append 'CENSORED' to the result list, else append the word itself.

Or you can involve list comprehension and conditional expression to get more elegant version:

z4y4ts
  • 2,535
  • 4
  • 21
  • 24
  • 2
    It’s considered extremely bad manners to solve homework questions for other people. It negates the learning effect, is unfair to other pupils and it pisses tutors off. – Konrad Rudolph Nov 04 '10 at 14:28
  • Oops, sorry for that, I haven't seen these homework questions before. Will it be ok if I remove the code and leave only comments? – z4y4ts Nov 04 '10 at 14:35
  • @Konrad Rudolph: That may become harder given that meta tags, like "homework", are now being discouraged -- see [*The Death of Meta Tags*](http://blog.stackoverflow.com/2010/08/the-death-of-meta-tags). Of course there are tradeoffs, but overall I personally tend to agree with the reasoning. – martineau Nov 04 '10 at 18:12
  • absolutely. Help is always appreciated, *including* for homework questions. – Konrad Rudolph Nov 04 '10 at 19:08
0

You could use the handy built-in enumerate() function to step through the items in the list. For example:

def censor(words, nasty):
    for i,word in enumerate(words):
       if word...
martineau
  • 119,623
  • 25
  • 170
  • 301