Tuesday, 15 March 2011

iteration - How efficient is Python's 'in' or 'not in' operators? -


I have a list of more than 100000 values ​​and I am iterating over these values ​​and checking that each other The list is included or not random values ​​(of the same size).

I am doing this by using if the item [x] in the random list How efficient is it? Does the python have some type of hashing for every container or internally I am searching directly to other containers to find that element?

In addition, if it does the search in a linear form, then what does it make a dictionary of random lists and lookup with it?

in is implemented by The magic method that applies to it, so the efficiency depends on it. For example, there will be set , dict and frozenset hash based lookups, while the list will require linear search However, xrange (or category in Python 3.x) contains a __ method that does not require linear search, but its Instead of using the start / stop / step information to determine the validation value. (Example: 7 in xrange (4, 1000000) is not linearly done).

Custom sections are free to implement __ contains __ however they look fit, but ideally should provide some information about how this happens in the document if " not clear".

No comments:

Post a Comment