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...") |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 20: | Line 20: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn to access and change: | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn to access and change: | ||
− | * Individual '''elements''' of single dimensional | + | * Individual '''elements''' of '''single dimensional''' and '''multi-dimensional '''arrays'''. |
* '''Rows''' and '''columns''' of '''arrays'''. | * '''Rows''' and '''columns''' of '''arrays'''. | ||
− | * Elements of an array, using '''slicing''' and '''striding'''. | + | * '''Elements''' of an '''array''', using '''slicing''' and '''striding'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 35: | Line 33: | ||
* '''Python 3.4.3 '''and | * '''Python 3.4.3 '''and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 78: | Line 74: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''import numpy as np''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''import numpy as np''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us create the two arrays in terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us create the two '''arrays''' in '''terminal'''. |
− | For this, we have to import '''numpy''' | + | For this, we have to import '''numpy library'''. |
− | Type | + | Type '''import numpy as np''' and press '''Enter'''. |
Line 91: | Line 87: | ||
|- | |- | ||
| 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 = np.arange(1,6)''' | | 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 = np.arange(1,6)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type | + | | 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 '''''is equal to''''' np '''''dot '''''arange '''''inside parentheses '''''1 '''''comma '''''6 ''' |
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C = np.arange(1,26).reshape(5,5)''' |
Line 101: | Line 97: | ||
− | We have already learnt about '''arange | + | We have already learnt about '''arange''' and''' reshape methods''' in an earlier tutorial. |
|- | |- | ||
Line 107: | Line 103: | ||
| 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 | + | |
+ | 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, '''C ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C ''' |
|- | |- | ||
Line 128: | Line 125: | ||
<nowiki>[21, 22, 23, 24, 25]])</nowiki> | <nowiki>[21, 22, 23, 24, 25]])</nowiki> | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''A''', we have only one row with elements from 1 to 6. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''A''', we have only one row with '''elements''' from 1 to 6. |
− | '''A''' is a '''one | + | '''A''' is a '''one dimensional array.''' |
− | In '''C''', we have 1 to 26 elements in the form of matrix of 5 rows and 5 columns. | + | In '''C''', we have 1 to 26 '''elements''' in the form of '''matrix''' of 5 rows and 5 columns. |
− | So '''C''' is a '''two | + | So '''C''' is a '''two dimensional array.''' |
Line 153: | Line 150: | ||
− | To access, the element 3 in | + | To access, the '''element''' 3 in '''array A''', we say, A of 2. |
Line 159: | Line 156: | ||
− | In Python, arrays are zero-indexed. | + | In '''Python, arrays''' are '''zero-indexed'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | This means, the position of the '''element''' starts with 0 instead of 1. | ||
|- | |- | ||
Line 172: | Line 166: | ||
Type, '''<nowiki>C[2, 3]</nowiki>''' | Type, '''<nowiki>C[2, 3]</nowiki>''' | ||
− | | 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 access the '''element''' 14 from | + | | 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 access the '''element''' 14 from '''array C'''. |
Line 194: | Line 188: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn how to change the value of an array. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn how to change the value of an '''array'''. |
Line 200: | Line 194: | ||
− | For this we simply assign the new | + | For this we simply assign the new value after accessing the '''element'''. |
Line 214: | Line 208: | ||
− | Type | + | Type '''A''' |
|- | |- | ||
Line 221: | Line 215: | ||
Highlight -3 and -14 | Highlight -3 and -14 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C''' |
− | You can see that the elements are changed now. | + | You can see that the '''elements''' are changed now. |
− | Likewise you can change any single | + | Likewise you can change any single '''element''' in an '''array'''. |
Line 246: | Line 240: | ||
− | Type | + | Type '''C '''''inside square brackets '''''2''' |
− | We can see that the third row of the array is displayed now. | + | We can see that the third row of the '''array''' is displayed now. |
|- | |- | ||
Line 258: | Line 252: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Python programming supports negative indexing of arrays. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Python programming''' supports '''negative indexing''' of '''arrays'''. |
Line 265: | Line 259: | ||
* -1 gives the last element and | * -1 gives the last element and | ||
* -2 gives the second to last element of an array. | * -2 gives the second to last element of an array. | ||
− | |||
− | |||
|- | |- | ||
Line 280: | Line 272: | ||
Highlight the outputs | Highlight the outputs | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Or''' ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Or with '''negative indexing''' as''' C '''''inside square brackets minus '''''1''' |
Line 300: | Line 292: | ||
− | Notice that | + | Notice that zeros are displayed in the last row of the '''array''' C. |
|- | |- | ||
Line 324: | Line 316: | ||
<nowiki>[start:stop]</nowiki> | <nowiki>[start:stop]</nowiki> | ||
− | | 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 learn to slice an array. | + | | 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 learn to '''slice''' an '''array'''. |
− | Slicing of an array is done to access parts of an array. | + | '''Slicing''' of an '''array''' is done to access parts of an '''array'''. |
− | Slicing syntax is inside square brackets '''start | + | '''Slicing''' syntax is inside square brackets '''start colon stop.''' |
|- | |- | ||
Line 337: | Line 329: | ||
syntax '''<nowiki>[start:stop:step]</nowiki>''' | syntax '''<nowiki>[start:stop:step]</nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Striding uses the ‘step’ value to jump between the elements in an array | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Striding''' uses the ‘step’ value to jump between the '''elements''' in an '''array'''. |
− | Striding syntax is inside square brackets '''start '''colon '''stop | + | '''Striding''' syntax is inside square brackets '''start '''colon '''stop colon step.''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal. | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal'''. |
|- | |- | ||
Line 354: | Line 346: | ||
− | 0 and 3 corresponds to start and stop values for row slicing and 2 corresponds to column index. | + | 0 and 3 corresponds to start and stop values for row '''slicing''' and 2 corresponds to column index. |
Line 360: | Line 352: | ||
− | Hence we have sliced the array. | + | Hence we have '''sliced''' the '''array'''. |
|- | |- | ||
Line 379: | Line 371: | ||
− | 2 corresponds to row index and 0 and 3 corresponds to start and stop values for column slicing. | + | 2 corresponds to row index and 0 and 3 corresponds to start and stop values for column '''slicing'''. |
|- | |- | ||
Line 392: | Line 384: | ||
− | Obtain the following elements one by one from array C. | + | Obtain the following '''elements''' one by one from '''array''' C. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 406: | Line 398: | ||
− | We get the elements 7 and 8. | + | We get the '''elements''' 7 and 8. |
|- | |- | ||
Line 416: | Line 408: | ||
− | We get the elements 1, 6, 11 and 16 | + | We get the '''elements''' 1, 6, 11 and 16 |
|- | |- | ||
Line 426: | Line 418: | ||
− | We get the elements 6, 11, 16 and 2 | + | We get the '''elements''' 6, 11, 16 and 2 |
|- | |- | ||
| 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 443: | Line 435: | ||
− | + | Obtain the '''elements''' [[8, 9], [13, -14]] from '''array''' C. | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 496: | Line 488: | ||
Highlight the output | Highlight the output | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The step 2 specifies the jump between the elements. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''step''' 2 specifies the '''jump''' between the '''elements'''. |
Line 502: | Line 494: | ||
− | If no step is specified, a default value of 1 is assumed. | + | If no '''step''' is specified, a default value of 1 is assumed. |
Line 508: | Line 500: | ||
− | We get the elements | + | We get the '''elements''' as shown. |
|- | |- | ||
Line 521: | Line 513: | ||
− | Obtain the following elements from array C. | + | Obtain the following '''elements''' from '''array''' C. |
|- | |- | ||
Line 544: | Line 536: | ||
# Access and change '''rows''' and '''columns''' of arrays by specifying the '''row''' and '''column''' numbers. | # Access and change '''rows''' and '''columns''' of arrays by specifying the '''row''' and '''column''' numbers. | ||
# '''Slice''' and '''stride''' on '''arrays'''. | # '''Slice''' and '''stride''' on '''arrays'''. | ||
− | |||
− | |||
|- | |- | ||
Line 554: | Line 544: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | # 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? | ||
|- | |- | ||
Line 570: | Line 558: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers, | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers, |
− | # 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 |
|- | |- |
Latest revision as of 22:00, 6 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. |