18

I'm using Python to dig through a pretty big project and dig up information about it. I'm able to create an array of ProjectFiles, however I'm having a hard time figuring out how to filter it.

class ProjectFile:
    def __init__(self, filename: str,
                 number_of_lines: int,
                 language: str,
                 repo: str,
                 size: int):
        self.filename = filename
        self.number_of_lines = number_of_lines
        self.language = language
        self.repo = repo
        self.size = size

How would I filter an array of ProjectFile objects for a specific repo?

For instance, let's say I wanted to filter for objects whose repo property is SomeCocoapod.

I've looked for examples of filter, but everything I've found uses simple examples like lists of str or int.

2 Answers 2

21

You can select attributes of a class using the dot notation.

Suppose arr is an array of ProjectFile objects. Now you filter for SomeCocoapod using.

filter(lambda p: p.repo == "SomeCocoapod", arr)

NB: This returns a filter object, which is a generator. To have a filtered list back you can wrap it in a list constructor.

As a very Pythonic alternative you can use list comprehensions:

filtered_arr = [p for p in arr if p.repo == "SomeCocoapod"]
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! On filtering with lambda, I needed to wrap the filter in a list and it works perfectly :)
3

Lets say you had this simple list of two ProjectFile objects:

projects = [
    ProjectFile(
        filename="test1.txt",
        number_of_lines=1,
        language="English",
        repo="repo1",
        size=1,
    ),
    ProjectFile(
        filename="test2.txt", 
        number_of_lines=2, 
        language="German", 
        repo="repo2", 
        size=2
    ),
]

You could then filter out repo1 using the repo attribute inside a list comprehension:

filtered = [project for project in projects if project.repo == "repo1"]

The above assumes you have overriden __str__ or __repr__ inside your ProjectFile class to give you a string representation of the filtered objects. Otherwise you will get something like [<__main__.ProjectFile object at 0x000001E879278160>] returned(which is fine if that's what you want to see). You can have a look at How to print instances of a class using print()? for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.