Hi, I am trying to split a string and return specific words.
I know that using string.split() turns the string into a list of specific words.
But how would I go about returning specific words? like the ones that contain certain letters?

This is my code so far:

def e_words(m):
      forbid == e
      word == m.split()
      if forbid in m:
          return forbidword

I want forbidword to be the words that contain the letter e.
How would I achieve that?

Edited 6 Years Ago by HoneyBadger: n/a

Now your function is basically doing nothing right. Start from beginning and test and add functionality. Step one: make this print string word, if string forbid is in word

forbid = 'e'
word = 'test'
if # your code

lol. Sorry Tony

def e_words(m):
      forbid = 'e'
      word = m.split()
      if forbid in word:
          return forbidword

I can't figure out what forbidword should be.

m.split() is not word. I suggest to write the simple code I asked, and go step by step.

I know word is not m.split().

word is basically, the user's string turned into a list of 'words'. hence the name word.

example: user_input = raw_input("Enter a string: ") suppose user enters: "This is a string"

word ==


I want to search that list, and return the words that contain the letter 'e'.

Add some printing to figure out what you are testing. Link to iterating through lists.

def e_words(m):
      forbid = 'e'
      word = m.split()
      print "testing", forbid, "in", word
      if forbid in word:
          return forbidword

Add some printing to figure out what you are testing. Link to iterating through lists.

def e_words(m):
      forbid = 'e'
      word = m.split()
      print "testing", forbid, "in", word
      if forbid in word:
          return forbidword

Ok. I did. It returns:
"testing" e in

But I don't know how forbidword should be written.
forbidword is supposed to be the words in the list that contain the letter 'e'.

Edited 6 Years Ago by HoneyBadger: n/a

In addition to the iterating through lists link above, there is also list comprehension which is the simplest way to do this.

testing = ['This', 'is', 'a', 'string'
print [x for x in testing if "t" in x.lower()]

Edited 6 Years Ago by woooee: n/a

In addition to the iterating through lists link above, there is also list comprehension which is the simplest way to do this.

testing = ['This', 'is', 'a', 'string'
print [x for x in testing if "t" in x.lower()]

I typed that in into my interpreter and this is what I got:

# remember this is the string.
testing = ['This', 'is', 'a', 'string']

print [x for x in testing if "t" in x.lower()]

['This', 'string']

So not to pass up a great opportinuy to learn, wooee. What is going on here:

[x for x in testing if "t" in x.lower()]

Edited 6 Years Ago by HoneyBadger: n/a

In addition to the iterating through lists link above, there is also list comprehension which is the simplest way to do this.

testing = ['This', 'is', 'a', 'string'
print [x for x in testing if "t" in x.lower()]

I took woooee's code and I am trying to iterate a list of characters:
When I go and run this, it only checks the first letter in the list of characters(forbidlist)

What happened?

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

#This is what I got
def iter_list(a):
    for s in a:
        return s

#checks to see if the given string contains the forbidden letters.
def avoids(m,f):
    forbidlist = list(f.lower())
    userlist = m.split()
    wordswithforbid = [x for x in userlist if iter_list(forbidlist) in x.lower()]
    wordsnonforbid = list(set(userlist).difference(wordswithforbid))
    number_of_non = len(wordsnonforbid)
    return str(number_of_non) + " words don't have the forbidden letters."

iter_list is not iterator it is function, which returns first letter of the sequence a, error if a is not iterable (for example None) and None for empty sequence. One step at time!

iter_list is not iterator it is function, which returns first letter of the sequence a, error if a is not iterable (for example None) and None for empty sequence. One step at time!

Does Python have a built in iterator?
I was in the python interpreter and I typed in help(list)
I was looking through what came up, and I came across iter(x) - is that the iterator?
I tried using it and all I got was: <listiterator object at 0xb77029ac>

Most cases sequence behaves enough like iterator:

>>> values='a','b','c'
>>> for item in values:
    print item

Most cases sequence behaves enough like iterator:

>>> values='a','b','c'
>>> for item in values:
    print item

How do I turn my list: forbidlist
into just values(no brackets?)

You do not need to:

forbid = ['a','b','c']
words = ['apple', 'orange','Kiwi', 'Banana']
for character in forbid:
    for word in words:
        if character in word.lower():
            print character, word
This article has been dead for over six months. Start a new discussion instead.