Files
example-code-2e/02-array-seq/memoryviews.ipynb
2021-02-14 20:28:07 -03:00

4.1 KiB

memoryviews

References

  • PEP-3118 Revising the buffer protocol (2006)
  • issue14130 memoryview: add multi-dimensional indexing and slicing (2012)
In [1]:
import sys
print(sys.version)
3.7.1 (default, Dec 14 2018, 13:28:58) 
[Clang 4.0.1 (tags/RELEASE_401/final)]
In [2]:
mv1d = memoryview(bytes(range(35, 50)))
mv1d
Out[2]:
<memory at 0x1045a31c8>
In [3]:
list(mv1d)
Out[3]:
[35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
In [4]:
mv2d = mv1d.cast('B', [3, 5])
mv2d
Out[4]:
<memory at 0x10464a120>
In [5]:
mv2d.shape
Out[5]:
(3, 5)
In [6]:
len(mv2d)
Out[6]:
3
In [7]:
mv2d.tolist()
Out[7]:
[[35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49]]
In [8]:
for row in mv2d.tolist():
    print(row)
[35, 36, 37, 38, 39]
[40, 41, 42, 43, 44]
[45, 46, 47, 48, 49]
In [9]:
mv2d[1, 2]
Out[9]:
42
In [10]:
mv2d.tolist()[1][2]
Out[10]:
42