A Python range
is an interesting
object because it behaves like a generator when it comes to memory, but it otherwise
behaves like a sequence and it additionally has O(1)
time
complexity for in
operation, .index()
and .count()
.
This is
an example of a dynamic sequence, i.e. a href="https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence"
rel="nofollow noreferrer">Sequence
object that
does not store its elements in memory.
How do I
implement a dynamic sequence in
Python?
The
in
operation, .index()
and
.count
methods implemented in O(1)
time, would, of course, be a nice addition.
/>
For concreteness, let us consider the case of a
Geometric
sequence:
s_k = a * r **
k
where
k
is an
integer.
Obviously, one cannot simply
use generator (simplified)
like:
def geom_seq(a, r, start,
stop=None):
if stop is None:
start, stop = 0, start
else:
start, stop = start, stop
item = a * r ** start
for _ in range(start, stop):
yield item
item *=
r
because this, while
being memory efficient would not behave like a Sequence
,
e.g.:
a = geom_seq(1, 2,
8)
len(a)
#
TypeError...
list(a)
# [1, 2, 4, 8, 16, 32,
64, 128]
list(a) # generator is consumed...
#
[]
On the contrary,
something like:
a =
tuple(geom_seq(1, 2,
8))
will
be(have like) a Sequence
but would not be memory
efficient:
import
sys
print(sys.getsizeof(geom_seq(1, 2,
1000)))
# 88
print(sys.getsizeof(tuple(geom_seq(1, 2,
1000))))
#
8048
/>
This is not a duplicate of
href="https://stackoverflow.com/questions/7875911/how-to-implement-a-minimal-class-that-behaves-like-a-sequence-in-python">How
to implement a minimal class that behaves like a sequence in Python? as time /
memory considerations are not discussed there.
No comments:
Post a Comment