-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumerate.py
51 lines (39 loc) · 1.1 KB
/
enumerate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def genumerate(arg, i=0):
for x in arg:
yield i, x
i += 1
import itertools
def ienumerate(arg, i=0):
return itertools.izip(itertools.count(i), arg)
af = ienumerate(range(3))
class cenumerate_fail:
# Bzzzst. Haha oh wow.
def __init__(self, data, start=0):
self._i = start
self._data = data
self._c = 0
def __iter__(self):
return self
def next(self):
try:
return self._i+self._c, next(self._data)
except TypeError:
try:
return self._i+self._c, self._data[self._c]
except IndexError:
raise StopIteration
finally:
self._c += 1
class cenumerate:
def __init__(self, data, start=0):
# The trick is to use the built in iter() function
# WELL THAT MAKES IT EASIER
self._iter = iter(data)
self._i = start
def next(self):
self._i += 1
return self._i-1, next(self._iter)
def __iter__(self):
return self
xs = cenumerate('hello', 3)
ys = enumerate('hello', 3)