Slices you hear when you're learning python.
Because the behavior changes depending on the range specification and the amount of change specified by plus and minus.
Check each pattern to see what happens when the range is specified, the amount of change is set to plus or minus, or when the range is specified outside the range, and unintended behavior is prevented.
[:]
Not specified](# 1-All not specified)[a:]
Specify only start](# 2-a Specify only start)[: b]
Specify only the end](# 3-b Specify only the end)[a: b]
Specify start and end](# 4-ab Specify start and end)[a: b: c]
Specify start, end, change amount](# 5-abc specify start end end change amount)[a :: c]
Start, specify change amount](# 6-ac Specify start change amount)[: b: c]
End, specify change amount](# 7-bc Specify end change amount)[:: c]
Specify only the amount of change](# 8-c Specify only the amount of change)A type of ** range specification method ** such as list.
Notation to specify the range with []
and :
.
Things like [1: 5] and [2:] that you often see in list and for statements.
It can be used not only for list but also for character strings (str), tuples, sets, ranges, etc.
Cut out (slice) consecutive elements and characters at any place.
If the starting value is negative, ** the allocation of array numbers will change **. └ With the last element as -1, count by -1. └ The number of the first element is the smallest
The conditions for "a" and "b" change depending on whether the amount of change "c" is positive or negative . └ Plus change: Always " a <b " └ The amount of change is negative: always " a> b **" └ No error will occur even if it is not the above. There is no corresponding data (empty).
** Does not include the value specified by "b" **. └ To end with the specified value.
[:]
: Not specified = All[a:]
: Specify only the start[: b]
: Specify only the end[a: b]
: Specify start and end[a: b: c]
: Specify start, end, change amount[a :: c]
: Start, specify the amount of change[: b: c]
: End, specify the amount of change[:: c]
: Specify only the amount of change** Supplement **
9. [a ::]
: Specify only the start (same as [a:])
10. [: b:]
: Specify only the start (same as [: b])
(1) When the start value is positive 1-1. List type 1-2. Characters
(2) When the start value is negative 2-1. list type 2-2. Characters
(3) When the start price is minus and the end price is plus
(4) Concept when specifying outside the range
Example 1: When ʻa = [1,2,3,4,5]`
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number | 0 | 1 | 2 | 3 | 4 |
data | 2020 | 3 | 25 | Year | Month | Day |
---|---|---|---|---|---|---|
Sequence number | 0 | 1 | 2 | 3 | 4 | 5 |
data | T | h | i | s | i | s | s | l | i | c | e | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sequence number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
data | This | Re | But | 、 | 「 | Su | La | I | Su | 」 | so | Su | 。 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sequence number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
Example 1: When ʻa = [1,2,3,4,5]`
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number | -5 | -4 | -3 | -2 | -1 |
data | 2020 | 3 | 25 | Year | Month | Day |
---|---|---|---|---|---|---|
Sequence number | -6 | -5 | -4 | -3 | -2 | -1 |
data | T | h | i | s | i | s | s | l | i | c | e | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sequence number | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
data | This | Re | But | 、 | 「 | Su | La | I | Su | 」 | so | Su | 。 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sequence number | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
** ■ Way of thinking ** Whether it is plus or minus depends on which method ** the number is specified **.
-If it is negative, refer to the data corresponding to the sequence number (-).
-If positive, refer to the data corresponding to the sequence number (+).
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(+) | 0 | 1 | 2 | 3 | 4 |
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
⇒ 3 to less than 5. The output is [3, 4].
If the start and end values are mixed, it is easier to understand by replacing them with positive numbers, which correspond to negative numbers.
・ ` "-3: 4" ` = ` "2: 4" ` ・ ` "-5: 4" ` = ` "0: 4" ` ・ ` "1: -1" ` = ` "1: 5" `
[:]
: Not specified = All[a:]
: Specify only the start[: b]
: Specify only the end[a: b]
: Specify start and end[a: b: c]
: Specify start, end, change amount[a :: c]
: Start, specify the amount of change[: b: c]
: End, specify the amount of change[:: c]
: Specify only the amount of changelist (numerical value)
a = [1, 2, 3, 4, 5]
a[:]
#output
# [1, 2, 3, 4, 5]
letter
c = 'This is slice'
c[:]
#output
# 'This is slice'
Character (directly specified)
'This is slice'[:]
#output
# 'This is slice'
▼ Sequence number 2nd or higher (2 to 4)
Designated by plus
a = [1, 2, 3, 4, 5]
a[2:]
#output
# [3, 4, 5]
▼ SEQ ID NO: -4th or higher (-4 to -1)
Specify with minus
a = [1, 2, 3, 4, 5]
a[-4:]
#output
# [2, 3, 4, 5]
** ▼ (Supplement) Sequence number of each data **
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(+) | 0 | 1 | 2 | 3 | 4 |
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
▼ When "6" is specified as the start value in the list that has only "4".
(Outside the range) Designated by plus
a = [1, 2, 3, 4, 5]
a[6:]
#output
# []
No hit
(Outside the range) Specify with minus
a = [1, 2, 3, 4, 5]
a[-10:]
#output
# [1, 2, 3, 4, 5]
The -10 to -6th are empty. -The 5th or higher hit.
▼ Sequence number up to 3rd (0 to 2nd) └ Less than 3 (because it ends at the 3rd)
Designated by plus
a = [1, 2, 3, 4, 5]
a[:3]
#output
# [1, 2, 3]
▼ SEQ ID NO: -1st (-5 to -2) └ Less than -1 (because it ends at -1)
Specify with minus
a = [1, 2, 3, 4, 5]
a[:-1]
#output
# [1, 2, 3, 4]
** ▼ (Supplement) Sequence number of each data **
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(+) | 0 | 1 | 2 | 3 | 4 |
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
▼ Specify the sequence number from 1st to less than 4. └ 4 is not included (3 elements from 1 to 3)
Designated by plus
a = [1, 2, 3, 4, 5]
a[1:4]
#output
# [2, 3, 4]
▼ SEQ ID NO: -4th to less than -2 └ Does not include -2. (2 elements from -4 to -3)
Specify with minus
a = [1, 2, 3, 4, 5]
a[-4:-2]
#output
# [2, 3]
** ▼ (Supplement) Sequence number of each data **
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
If the starting value is negative, refer to ** Data corresponding to sequence number (-) ** for the starting value.
If the closing value is positive, refer to ** Data corresponding to the sequence number (+) ** for the closing value.
Designated by plus and minus
a = [1, 2, 3, 4, 5]
a[-3:4]
#output
# [3, 4]
[-3: 4]
does not specify -3 to 4 in succession. It is not "-3, -2, -1,0,1,2,3,4".If the start and end values are mixed, it is easier to understand by replacing them with positive numbers, which correspond to negative numbers.
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(+) | 0 | 1 | 2 | 3 | 4 |
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
・ ` "-5: 4" ` = ` "0: 4" ` ・ ` "1: -1" ` = ` "1: 5" ` ・ ` "-3: 2" ` = ` "2: 2" `
▼ Specify the 10th end value for the data that is only up to the 4th.
Specify out of range (plus)
a = [1, 2, 3, 4, 5]
a[3:10]
#output
# [4, 5]
The sixth and subsequent data are not applicable.
Specify out of range (minus)
a = [1, 2, 3, 4, 5]
a[-20:-3]
#output
# [1, 2]
No hits from -20 to -6th. Data from -5 to less than -3rd is output.
Specify out of range from out of range (plus)
a = [1, 2, 3, 4, 5]
a[10:30]
#output
# []
The output will be empty.
Specify out of range from out of range (minus)
a = [1, 2, 3, 4, 5]
a[-20:-10]
#output
# []
The output will be empty.
▼ Specify out of range from minus start value to plus end value.
Specify out of range from out of range (minus and plus)
a = [1, 2, 3, 4, 5]
a[-20:10]
#output
# [1,2,3,4,5]
The result is the same as [-5: 4]
= [0: 4]
.
-20
Less than -5 is because there is no data. -5 is the maximum value.
-5 corresponds to the 0th designation of plus.
10
Since there is no data after the 4th, 4 is the maximum value.
data | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Sequence number(+) | 0 | 1 | 2 | 3 | 4 |
Sequence number(-) | -5 | -4 | -3 | -2 | -1 |
▼ When the start value 1, end value 9 and change amount 2 are specified.
Specify start, end, change amount(plus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[1:9:2]
#output
# [2, 4, 6, 8]
Increase the sequence number from 1st to 2nd, and execute until less than 9. Extract the data corresponding to the number.
Specify start, end, change amount(minus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[8:3:-2]
#output
# [9, 7, 5]
Decrease the sequence number by 2 from the 8th, and finish when it comes to the 3rd (3rd is not included). Extract the data corresponding to the number.
data | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
Sequence number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Specify start, end, change amount(minus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[-3:-8:-2]
#output
# [8, 6, 4]
Decrease the sequence number by 2 from the 3rd, and finish when it comes to the 8th (-8th is not included). Extract the data corresponding to the number.
data | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
Sequence number | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
▼ When the start value 1 and the amount of change 3 are specified
Specify only start and change amount(plus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[1::3]
#output
# [2, 5, 8]
Increase the number from 1st to 3rd and execute until there is data.
Specify only start and change amount(minus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[5::-1]
#output
# [6, 5, 4, 3, 2, 1]
Decrease the number from 5th to 1st, and execute until there is data.
** Starting value is different ** depending on whether the amount of change is positive or negative.
▼ When the end value 5 and the amount of change 2 are specified (start value 0)
Specify only the end and change amount(plus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[:5:2]
#output
# [1, 3, 5]
** Start value 0th ** Increase the number by 2 and execute to less than 5th (5th is not included).
Specify only the end and change amount(plus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[:5:-2]
#output
# [10, 8]
** Start value 9th ** Decrease the number by 2 and execute to less than 5th (5th is not included).
The starting value is different depending on whether the amount of change is ** plus ** or ** minus **.
▼ When the amount of change is positive (starting value 0)
Specify only the amount of change(plus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[::3]
#output
# [1, 4, 7, 10]
** Start value 0th ** Increase the number by 3 and execute until there is data.
Specify only the amount of change(minus)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[::-3]
#output
# [10, 7, 4, 1]
** Starting value 1
Decrease the number from 0th ** to 3rd, and execute until there is data.
Error case (int type)
1001[2:]
#output
# TypeError: 'int' object is not subscriptable
Error case (float type)
123.45[2:5]
#output
# TypeError: 'float' object is not subscriptable
Recommended Posts