forked from Qwertyssp/python_ebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslicing1d.py
More file actions
39 lines (29 loc) · 647 Bytes
/
slicing1d.py
File metadata and controls
39 lines (29 loc) · 647 Bytes
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
import numpy as np
# Chapter 2 Beginning with NumPy fundamentals
#
# Demonstrates one dimensional arrays slicing.
#
# Run from the commandline with
#
# python slicing1d.py
print "In: a = arange(9)"
a = np.arange(9)
print "In: a[3:7]"
print a[3:7]
#Out: array([3, 4, 5, 6])
print "In: a[:7:2]"
print a[:7:2]
#Out: array([0, 2, 4, 6])
print "In: a[::-1]"
print a[::-1]
#Out: array([8, 7, 6, 5, 4, 3, 2, 1, 0])
print "In: s = slice(3,7,2)"
s = slice(3,7,2)
print "In: a[s]"
print a[s]
#Out: array([3, 5])
print "In: s = slice(None, None, -1)"
s = slice(None, None, -1)
print "In: a[s]"
print a[s]
#Out: array([8, 7, 6, 5, 4, 3, 2, 1, 0])