5,337
edits
Line 263: | Line 263: | ||
===iterators and iterables=== | ===iterators and iterables=== | ||
iterators | Iterables include lists, np arrays, tuples. | ||
To create an iterator, pass an iterable to the <code>iter()</code> function. | |||
<syntaxhighlight lang="python"> | |||
my_arr = [1,2,3,4] | |||
my_iter = iter(my_arr) | |||
v1 = my_iter.next() | |||
</syntaxhighlight> | |||
<code>itertools</code> contains many helper functions for interacting with iterables and iterators. | |||
====zip==== | ====zip==== | ||
Line 278: | Line 287: | ||
i.e. enumerate([a1,...], start=0) = [(0, a1), (1, a2), ...] | i.e. enumerate([a1,...], start=0) = [(0, a1), (1, a2), ...] | ||
====slice==== | |||
<code>itertools.islice</code> will allow you to create a slice from an iterable | |||
<syntaxhighlight lang="python"> | |||
from itertools import islice | |||
import numpy as np | |||
a = np.arange(5) | |||
b = islice(a, 3) | |||
list(b) # [0,1,2] | |||
</syntaxhighlight> | |||
==Classes== | ==Classes== |