Can I track the progress of my filter function?

时间:2016-02-12 22:05:03

标签: python

I have a rather large list. Most of the elements of the list are not of interest to me, so I'd like to filter it to find the elements I care about. My python code is something like

$(workspaceRoot}

Since my list is long, I'd like to be able to track the progress of my program.

Are there any tools that allow me to do this?

2 个答案:

答案 0 :(得分:3)

{{#each data.articles}}
<article class="id-{{this.id}}">
    <h1><a href="/journal/{{this.url}}">{{this.title}}</a></h1>
    <p>{{this.body}}</p>
</article>

{{else}}
    <p class="empty">No content</p>
{{/each}}

its not very elegant but you get the idea im sure

a slightly more pythonic alternative ...

count = 0
def my_criteria(x):
    global count
    count += 1
    print count+"/"+len(L)
    return True or False
def get_interesting_elements(L)
    return filter(lambda x: my_criteria(x) == True, L)

答案 1 :(得分:0)

此解决方案不需要将进度代码包含在您的条件函数中,并且可以与其他条件函数一起使用:

def show_progress(function, L):
    def wrapper(elem):
        i, x = elem
        print('{}/{}'.format(i, len(L)))
        return function(x)
return wrapper

def get_interesting_elements(L)
    return map(lambda x: x[1], filter(show_progress(my_criteria, L), enumerate(L)))

但这是相当详细的。我建议使用循环手动过滤列表。