문제

Python has enumerate() to iterate over objects with an index.I doubt that interpreters create a lot of int objects for the sole purpose of keeping track of where things are. The PEP page says the following, but I do not really understand what is going on under the hood:

It provides all iterables with the same advantage that iteritems() affords to dictionaries -- a compact, readable, reliable index notation.

So what is the magic here?

도움이 되었습니까?

해결책

enumerate() is an iterator; it only produces the index int value on the fly; it does not produce them all up front.

You can try to read the enumobject.c source code, but it basically can be translated to Python like this:

def enumerate(iterable, start=0):
    count = start
    for elem in iterable:
        yield count, elem
        count += 1

The yield keyword makes this a generator function, and you need to loop over the generator (or call next() on it) to advance the function to produce data, one yield call at a time.

Python also interns int values, all values between -5 and 256 (inclusive) are singletons, so the above code doesn't even produce new int objects until you reach 257.

다른 팁

It helps you know where things are....

l = ['apple', 'banana', 'cabbage']

for idx, item in enumerate(l):
    print "the item: %s, is at position %s" % (item, idx)

>>> 
the item: apple, is at position 0
the item: banana, is at position 1
the item: cabbage, is at position 2

This helps in the following scenario.. Imagine you want to find every 'cabbage' item in the list. And know their indexes.

l = ['apple', 'banana', 'cabbage', 'monkey', 'kangaroo', 'cabbage']

def find_indexes(lst, match):
    results = []
    for idx, item in enumerate(l):
        if item == match:
            results.append(idx)
    return results

print find_indexes(l, 'cabbage')

>>> 
[2, 5]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top