Difference between revisions of "Python-3.4.3/C2/Accessing-parts-of-arrays/English"
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...") |
|||
Line 101: | Line 101: | ||
− | We have already learnt about '''arange | + | We have already learnt about '''arange''' and''' reshape methods''' in an earlier tutorial. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''A''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''A''' | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us see the contents of A and C. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us see the contents of A and C. | ||
+ | |||
Type, '''A ''' | Type, '''A ''' | ||
Line 430: | Line 431: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>C[1:, 0]</nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>C[1:, 0]</nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also get the same elements by typing '''C '''''square brackets '''''1 '''colon comma''' 0''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also get the same elements by typing '''C '''''inside square brackets '''''1 '''colon comma''' 0''' |
|- | |- | ||
Line 558: | Line 559: | ||
# How do we access the element 18 from the given array A? | # How do we access the element 18 from the given array A? | ||
− | # How do we obtain the elements <nowiki>[[21, 22], [31, 32]] </nowiki>from the given array B | + | # How do we obtain the elements <nowiki>[[21, 22], [31, 32]] </nowiki>from the given array B? |
Line 574: | Line 575: | ||
# The element 18 in array A has index number 2. Hence, we can access it as A of 2 | # The element 18 in array A has index number 2. Hence, we can access it as A of 2 | ||
− | # To obtain the central four numbers in the array B, we say, | + | # To obtain the central four numbers in the array B, we say, B ''inside square brackets ''1 colon 3 comma 1 colon 3 |
|- | |- |
Revision as of 16:38, 4 May 2018
|
|
Show Slide
|
Welcome to the spoken tutorial on "Accessing parts of arrays". |
Show Slide
Objectives
|
In this tutorial, we will learn to access and change:
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite slide |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show Slide
Sample Arrays
|
Let us begin with the help of an example.
|
[Terminal]
Open the terminal, type ipython3 and press Enter |
Let us start ipython.
|
Type import numpy as np | Let us create the two arrays in terminal.
|
Type, A = np.arange(1,6) | Type, A is equal to np dot arange inside parentheses 1 comma 6 |
Type, C = np.arange(1,26).reshape(5,5)
|
Type, C is equal to np dot arange inside parentheses 1 comma 26 dot reshape inside parentheses 5 comma 5
|
Type A | Now, let us see the contents of A and C.
|
Type, C | Type, C |
Highlight
A = array([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]]) |
In A, we have only one row with elements from 1 to 6.
|
Highlight 3 in A = array([1, 2, 3, 4, 5])
|
Next, let us see about accessing individual elements in an array.
|
Highlight array C in slide
|
Now, let us access the element 14 from array C.
|
Highlight array A, C in slide
|
Next we will learn how to change the value of an array.
|
Type, C[2, 3] = -14 | Type, C inside square brackets 2 comma 3 is equal to minus 14 |
Type, A | Let us check our operations.
|
Type C
|
Type, C
|
Highlight array C in slide
|
Next let us learn to change more than one elements at a time.
|
Slide:
Negative Indexing
|
Python programming supports negative indexing of arrays.
|
Type C[4] | We can access the last row of C in 2 ways.
|
Type C[-1]
|
Or with negative indexing as C inside square brackets minus 1
|
Type C[-1] = [0, 0, 0, 0, 0]
|
Now, we will learn to change the last row into all zeros.
|
Type C | Type C
|
Type C[-1] = 2 | We can also type, C inside square brackets minus 1 is equal to 2. |
Type C
|
Type C
|
Slicing slide
|
Now let us learn to slice an array.
|
Striding slide
|
Striding uses the ‘step’ value to jump between the elements in an array
|
Switch to terminal. | Switch back to the terminal. |
Type C[0:3,2]
|
Type, C inside square brackets 0 colon 3 comma 2
|
Highlight in C ([11, 12, 13])
|
Now we will access the elements of row with index 2, and first 2 columns.
|
Show Slide
|
Pause the video.
|
Switch to terminal | Switch to the terminal for the solution. |
Type C[1, 1:3]
|
Type C inside square brackets 1 comma 1 colon 3
|
Type C[0:4, 0]
|
Type C inside square brackets 0 colon 4 comma 0
|
Type C[1:5, 0]
|
Type C inside square brackets 1 colon 5 comma 0
|
Type C[1:, 0] | We can also get the same elements by typing C inside square brackets 1 colon comma 0 |
Show Slide
|
Pause the video.
|
Switch to terminal | Switch to the terminal for the solution. |
Type C[1:3, 2:4]
|
Type, C inside square brackets 1 colon 3 comma 2 colon 4
|
Type C[0:5:2, 0:5:2]
|
Next we will learn the idea of striding using the smaller array C.
|
Type C[::2, ::2]
|
We can also type C inside square brackets colon colon 2 comma colon colon 2
|
Highlight 2 in C[::2, ::2]
|
The step 2 specifies the jump between the elements.
|
Show Slide
|
Pause the video.
|
Show Slide Solution 3 | The solution is on your screen. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
|
Show Slide
|
And the answers,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide Acknowledgement | Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.
|
Show Slide
Thank You |
This is Priya from IIT Bombay signing off. Thanks for watching. |