Memo # 5 for Python beginners to read "Detailed Python Grammar"

Back number

-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"

Chapter 4 Sequence and Container Type

--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

Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo # 5 for Python beginners to read "Detailed Python Grammar"
Basic Python grammar for beginners
~ Tips for beginners to Python ③ ~
Memo to ask for KPI with python
Beginners read "Introduction to TensorFlow 2.0 for Experts"
[Python] Read images with OpenCV (for beginners)
The fastest way for beginners to master Python
For beginners to build an Anaconda environment. (Memo)
Python for super beginners Python for super beginners # Easy to get angry
python textbook for beginners
Try to calculate RPN in Python (for beginners)
Python basic grammar memo
Introduction to Programming (Python) TA Tendency for beginners
How to make Python faster for beginners [numpy]
[For beginners] How to use say command in python!
How to convert Python # type for Python super beginners: str
[For beginners] How to study Python3 data analysis exam
[For beginners] Learn basic Python grammar for free in 5 hours!
Python # How to check type and type for super beginners
Python memo (for myself): Array
Learning flow for Python beginners
Python Basic Grammar Memo (Part 1)
Python code memo for yourself
Python3 environment construction (for beginners)
3 Reasons Beginners to Start Python
Python basic grammar (miscellaneous) Memo (3)
Python #function 2 for super beginners
Python basic grammar (miscellaneous) Memo (2)
100 Pandas knocks for Python beginners
[Python] Sample code for Python grammar
Python #list for super beginners
Introduction to Python For, While
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
[For beginners] How to read Numerai's HP + Submit + Convenient links
[R] [Python] Memo to read multiple csv files in multiple zip files
How to learn TensorFlow for liberal arts and Python beginners
How to convert Python # type for Python super beginners: int, float
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
Tips for Python beginners to use the Scikit-image example for themselves
[Python] Introduction to graph creation using coronavirus data [For beginners]
Python Exercise for Beginners # 2 [for Statement / While Statement]
Minimum grammar notes for writing Python
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
Beginners use Python for web scraping (1)
[Nanonets] How to post Memo [Python]
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
An introduction to Python for non-engineers
python beginners tried to find out
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
[Pandas] I tried to analyze sales data with Python [For beginners]