0
def process_filter_description(filter, images, ial):
    '''Return a new list containing only items from list images that pass 
    the description filter (a str). ial is the related image association list.
    Matching is done in a case insensitive manner.
    '''

        images = []
        for items in ial:

Those are the only two lines of code I have so far. What is troubling me is the filter in the function. I really don't know what the filter is supposed to do or how to use it.

In no way am I asking for the full code. I just want help with what the filter is supposed to do and how I can use it.

4
  • 2
    Hmm -- this is really vague... more context will definitely be necessary to help you. Commented Nov 5, 2011 at 22:54
  • where is process_filter_description()? or is that what you are going to write? Commented Nov 5, 2011 at 22:55
  • 1
    This feels like homework to me. The doc string could be taken right out of a text book as a question. Commented Nov 5, 2011 at 22:59
  • 1
    One more thing, I'm not sure you want to accept a list as an argument and then immediately set it to an empty list (as you do with images. Commented Nov 5, 2011 at 23:11

2 Answers 2

1

Like I said in my comment, this is really vague. But I'll try to explain a little about the concept of a filter in python, specifically the filter() function.

The prototype of filter is: iterable <- filter(function, iterable).

iterable is something that can be iterated over. You can look up this term in the docs for a more exact explanation, but for your question, just know that a list is iterable.

function is a function that accepts a single element of the iterable you specify (in this case, an element of the list) and returns a boolean specifying whether the element should exist in the iterable that is returned. If the function returns True, the element will appear in the returned list, if False, it will not.

Here's a short example, showing how you can use the filter() function to filter out all even numbers (which I should point out, is the same as "filtering in" all odd numbers)

def is_odd(i): return i%2

l = [1,2,3,4,5] # This is a list
fl = filter(is_odd, l)
print fl # This will display [1,3,5]

You should convince yourself that is_odd works first. It will return 1 (=True) for odd numbers and 0 (=False) for even numbers.

In practice, you usually use a lambda function instead of defining a single-use top-level function, but you shouldn't worry about that, as this is just fine.

But anyway, you should be able to do something similar to accomplish your goal.

Sign up to request clarification or add additional context in comments.

Comments

0

Well it says in the description line:

Return a new list containing only items from list images that pass the description filter (a str)
...
Matching is done in a case insensitive manner

So.. im guessing the filter is just a string, do you have any kind of text associated with the images ? some kind of description or name that could be matched against the filter string ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.