Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Array slicing
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===1991: [[Python (programming language)|Python]]=== If you have the following list: <syntaxhighlight lang="pycon"> >>> nums = [1, 3, 5, 7, 8, 13, 20] </syntaxhighlight> Then it is possible to slice by using a notation similar to element retrieval: <syntaxhighlight lang="pycon"> >>> nums[3] # no slicing 7 >>> nums[:3] # from index 0 (inclusive) until index 3 (exclusive) [1, 3, 5] >>> nums[1:5] [3, 5, 7, 8] >>> nums[-3:] [8, 13, 20] </syntaxhighlight> Note that Python allows negative list indices. The index -1 represents the last element, -2 the penultimate element, etc. Python also allows a step property by appending an extra colon and a value. For example: <syntaxhighlight lang="pycon"> >>> nums[3:] [7, 8, 13, 20] >>> nums[3::] # == nums[3:] [7, 8, 13, 20] >>> nums[::3] # starting at index 0 and getting every third element [1, 7, 20] >>> nums[1:5:2] # from index 1 until index 5 and getting every second element [3, 7] </syntaxhighlight> The stride syntax (<code>nums[1:5:2]</code>) was introduced in the second half of the 1990s, as a result of requests put forward by scientific users in the Python "matrix-SIG" (special interest group).<ref name="millman">{{cite journal |first1=K. Jarrod |last1=Millman |first2=Michael |last2=Aivazis |title=Python for Scientists and Engineers |journal=Computing in Science and Engineering |volume=13 |number=2 |pages=9β12 |year=2011 |doi=10.1109/MCSE.2011.36 |bibcode=2011CSE....13b...9M |url=http://www.computer.org/csdl/mags/cs/2011/02/mcs2011020009.html}}</ref> Slice semantics potentially differ per object; new semantics can be introduced when [[operator overloading]] the indexing operator. With Python standard lists (which are [[dynamic array]]s), every slice is a copy. Slices of [[NumPy]] arrays, by contrast, are views onto the same underlying buffer.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)