简体   繁体   中英

Get length of a (non infinite) iterator inside it's loop using Python 2.7

I'm working with some iterator generated using itertools.imap , and I was thinking if there is a way to access the iterator length inside the for-loop that I use to loop over the elements.

What I can say for sure is that the iterator doesn't generate an infinite amount of data.

Also, because the information I'm looping are from a query to a database, I can get the length of the information from there, but the function I'm using has to return an iterator.

I thought of some options:

def iterator_function(some, arguments, that, I, need):
    query_result = query()
    return_iterator = itertools.imap(
        mapping_function,
        query_result
    )
    return return_iterator

Because I cannot change the returned iterator, I thought of something (really ugly) like:

query_result = query()
query_size = len(query_result)

return_iterator = itertools.imap(
    lambda item: (mapping_function(item), query_size),
    query_result
)

return return_iterator

But I don't really like this option, and I was thinking if there is a way, in Python, to get the iterator size from inside the loop over the iterator, something like:

for item in iterator():
    print item.iterator_length()
    # do other stuff

Or even something like:

for item in iterator():
    print iterator.length() # or iterator().length()???

Thanks!

I don't know if my idea is correct but how about class generator pattern and trying to add sequence bahaviour :

if your class represents something that has a length, don't define a GetLength method; define the __len__ method and use len(instance) .

something like this :

class firstn(object):
    def __init__(self, n):
        self.n = n
        self.num, self.nums = 0, []

    def __iter__(self):
        return self

    # Python 3 compatibility
    def __next__(self):
        return self.next()

    #    V------- Something like this 
    def __len__(self):
        return self.my_length

    def next(self):
        if self.num < self.n:
            cur, self.num = self.num, self.num+1
            return cur
        else:
            raise StopIteration()

Also, because the information I'm looping are from a query to a database, I can get the length of the information from there, but the function I'm using has to return an iterator.

Assuming you have a good database, do the count there. Doubt any solution in python will be faster/cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM