-Memo # 1 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 2 for Python beginners to read "Detailed Explanation of Python Grammar" -Memo # 3 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 4 for Python beginners to read "Detailed explanation of Python grammar"
--In Python, ordered collections, commonly referred to as arrays, are called ** sequences **. --There are various data types such as list type, tuple type, character string type, and byte type, but all of them can operate elements in the same way.
Access by subscript
>>> L = [0, 1, 2, 3, 4, 5]
>>> L[2]
2
#If you specify a negative value as the index, it will be referenced from the end.
>>> L[-1] #The index of the last element is-1
5
>>> L[-2]
4
#Exception if you specify an index outside the range
>>> L[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
slice
#Get all at once by specifying a range of index values
>>> L = [0, 1, 2, 3, 4]
>>> L[1:4]
[1, 2, 3] #Create a new sequence object with the elements up to the one before the element specified at the end position
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[-5:-1] #If the start position and end position are negative values, the position is specified from the end.
[5, 6, 7, 8]
>>> L[-5:0] #When you want to take it to the end, not like this
[]
>>> L[-5:] #like this
[5, 6, 7, 8, 9]
>>> L = [1, 2, 3]
>>> L[10:20] #Returns an empty sequence if you specify an index that does not exist
[]
>>> L[1:1] #Zero length slices also return an empty sequence
[]
>>> L[3:1] #A slice with a negative length also returns an empty string (it says empty string, but it's the same as above....?)
[]
>>> L[0:10] #If specified beyond the end of the element, returns up to the last element
[1, 2, 3]
>>> L[-10:10] #If specified beyond the beginning of the element, return from the first element
[1, 2, 3]
Get element by specifying incremental value
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[1:10:3] #Obtained by skipping index 3 between the start position and the end position
[1, 4, 7]
#If you specify a negative value as the increment value, the elements are fetched in reverse order from the back.
>>> L[8:2:-1]
[8, 7, 6, 5, 4, 3]
>>> L[10:1:-3]
[9, 6, 3]
Omission of slice specification
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[:5] #Omit the start position (start position is 0)
[0, 1, 2, 3, 4]
>>> L[0::2] #Omit the end position (the end position is 10)
[0, 2, 4, 6, 8]
#If the increment value is negative
>>> L[:5:-1] #Omit the start position (start position is 10)
[9, 8, 7, 6]
>>> L[10::-2] #Omit the end position (the end position is 0)
[9, 7, 5, 3, 1]
#If you omit the increment value, the increment value is 1.
>>> L[2:5:]
[2, 3, 4]
>>> L[2:5] #Last:Interpretation as a form that omits. I see.
[2, 3, 4]
#Omission tricks
>>> L[::2]
[0, 2, 4, 6, 8]
>>> L[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Slice object
#Slice operation[]The contents can be created as a slice object (useful when referencing a specific range many times)
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[slice(1, 10, 3)] # L[1:10:3]Same as
[1, 4, 7]
>>> L[slice(10)] # L[:10]Same as
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Substitution with slice
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[1:9] = ['spam', 'ham'] #Replaces elements in the slice range, lengths do not have to match
>>> L
[0, 'spam', 'ham', 9]
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[1:1] = ['spam', 'ham'] #For zero length slices, do not remove elements from the original list (insert only)
>>> L
[0, 'spam', 'ham', 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[3:-3] = [] #If the right-hand side is 0 in length, remove the element from the original list
>>> L
[0, 1, 2, 7, 8, 9]
#If you specify an increment value other than 1 for the slice, only the corresponding element is replaced with the value on the right side. It's cool.
>>> L = [0, 1, 2, 3, 4, 5, 6]
>>> L[1::2] = ['one', 'three', 'five']
>>> L
[0, 'one', 2, 'three', 4, 'five', 6]
#Just because the left side is a list does not mean that you cannot assign it unless it is a list.
#Any iterable object can be assigned.
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[5:] = 'spam'
>>> L
[0, 1, 2, 3, 4, 's', 'p', 'a', 'm']
Cumulative assignment statement
>>> L = [1, 2, 3]
>>> L[2] += 4
>>> L
[1, 2, 7]
>>> L = [0, 1, 2, 3]
>>> L[1:3] += ['spam', 'ham'] #You can also specify a slice on the left side
>>> L
[0, 1, 2, 'spam', 'ham', 3]
>>> L[3:5] *= 2 # L[3:5]To L[3:5]*Set 2.
>>> L
[0, 1, 2, 'spam', 'ham', 'spam', 'ham', 3]
Delete element
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del L[1], L[2]
>>> L
[0, 2, 4, 5, 6, 7, 8, 9]
#Of course you can specify slices and incremental values
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del L[::2]
>>> L
[1, 3, 5, 7, 9]
#Deleting an element does not free the object. Just remove it from the sequence.
>>> L1 = [1, 2, 3]
>>> L2 = [L1]
>>> L2
[[1, 2, 3]]
>>> del L2[0]
>>> L2
[]
>>> L1 #L1 is not deleted
[1, 2, 3]
Sequence operator
>>> [1, 2] + [3, 4] #Linking
[1, 2, 3, 4]
>>> [1, 2] * 3 #Repeat for the specified number of times
[1, 2, 1, 2, 1, 2]
>>> 3 * [1, 2] #Same even if the calculation order is changed
[1, 2, 1, 2, 1, 2]
#The comparison operator compares the elements of the sequence from the beginning.
>>> [1, 2] < [3, 4]
True # (1 < 3) & (2 < 4)
#in (it seems to be called a membership operator)
>>> 'B' in ['A', 'B', 'C']
True
Recommended Posts