Asdfg Adfg
Asdfg Adfg

Reputation: 51

Need to understand function in python

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.

Upvotes: 0

Views: 228

Answers (2)

jedwards
jedwards

Reputation: 30200

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.

Upvotes: 1

yurib
yurib

Reputation: 8147

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 ?

Upvotes: 0

Related Questions