Refer to O'Reilly Japan's "Introduction to Python 3" and the following Python Japanese translation document. Study the basic grammar of Python3 system. I hope it will be helpful for those who want to study Python in the same way.
Python 3.6.1 documentation https://docs.python.jp/3/index.html
The sequence type is a data structure for handling 0 or more elements. The basic sequence type corresponds to list, tuple, range, etc.
Lists are suitable for mutable sequences where the order and content may change. Also, the same value may appear more than once in the list. (If you don't care about the order if you can manage unique values, you may want to use a set)
>>> #Creating an empty array[]
>>> empty_list1 = []
>>> empty_list1
[]
>>> #List creation[]
>>> str_list1 = ['A','B','C']
>>> str_list1
['A', 'B', 'C']
>>> #Creation of empty array list()
>>> empty_list2 = list()
>>> empty_list2
[]
>>> #List creation list()
>>> str_list2 = list(['A','B','C'])
>>> str_list2
['A', 'B', 'C']
>>> #Convert strings to lists
>>> list('ABC')
['A', 'B', 'C']
>>> #Convert tuples to lists
>>> tuple_sample = ('A','B','C')
>>> list(tuple_sample)
['A', 'B', 'C']
>>> #Convert the split result of split to a list
>>> today = '2017/07/01'
>>> today.split('/')
['2017', '07', '01']
>>> #Convert split result to list (if empty string element is included)
>>> target = 'A//B/C/DE///F'
>>> target.split('/')
['A', '', 'B', 'C', 'DE', '', '', 'F']
>>> target.split('//')
['A', 'B/C/DE', '/F']
>>> target = ['A','B','C']
>>> #Specify a positive index
>>> target[0]
'A'
>>> target[1]
'B'
>>> target[2]
'C'
>>> #Specify a negative index (count in reverse order from the end)
>>> target[-1]
'C'
>>> target[-2]
'B'
>>> target[-3]
'A'
>>> #Error if offset out of range is specified
>>> target[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
List types can store elements of different types (including other list types).
>>> str_upper = ['A','B','C']
>>> str_lower = ['a','b','c']
>>> str_number = [1 ,'2','3',4]
>>> all_str = [str_upper, str_lower, str_number]
>>> #List of lists
>>> all_str
[['A', 'B', 'C'], ['a', 'b', 'c'], [1, '2', '3', 4]]
>>> #The first element from the list in the list
>>> all_str[0]
['A', 'B', 'C']
>>> #The second element from the list in the list
>>> all_str[1]
['a', 'b', 'c']
>>> #Third element from the list in the list
>>> all_str[2]
[1, '2', '3', 4]
>>> #If you specify two indexes, only the elements of the built-in list will be returned.
>>> all_str[1][0]
'a'
Since the list is mutable, you can rewrite the value by specifying the offset.
>>> target = ['A','B','C']
>>> target
['A', 'B', 'C']
>>> target[1] = 'V'
>>> target
['A', 'V', 'C']
>>> #Append to add elements to the list()Do by
>>> target[3] = 'D'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> target = ['A','B','C']
>>> target
['A', 'B', 'C']
>>> #From the beginning
>>> target[0:]
['A', 'B', 'C']
>>> #2 letters from the beginning
>>> target[0:2]
['A', 'B']
>>> #Reverse the elements of the list
>>> target[::-1]
['C', 'B', 'A']
>>> target = ['A','B','C']
>>> target.append('D')
>>> target
['A', 'B', 'C', 'D']
Since the append () function adds an element to the end, use this if you want to add an element by specifying an offset and an additional position. Also, if an offset beyond the end is specified, it will be added to the end of the list. (No exceptions can be thrown for incorrect offsets)
>>> target = ['A','B','C']
>>> #Added to the 3rd character
>>> target.insert(2,'Q')
>>> target
['A', 'B', 'Q', 'C']
>>> #If you specify an offset that exceeds the end
>>> target.insert(10,'Z')
>>> target
['A', 'B', 'Q', 'C', 'Z']
>>> #When counting from the end
>>> target.insert(-2,'V')
>>> target
['A', 'B', 'Q', 'V', 'C', 'Z']
Since del is a Python statement, not a list method, we don't write del ().
>>> target = ['A','B','C','D','E']
>>> #Delete the third character
>>> del target[2]
>>> target
['A', 'B', 'D', 'E']
>>> #Delete the end
>>> del target[-1]
>>> target
['A', 'B', 'D']
If you want to specify an element and delete it instead of specifying an offset, use this instead of del.
>>> target = ['A','B','C','D','E']
>>> target.remove('B')
>>> target
['A', 'C', 'D', 'E']
You can use pop () to retrieve an element from the list and remove it from the list at the same time. If no argument is specified, -1 (end) is specified as the offset.
>>> target = ['A','B','C','D','E','F','G']
>>> #Pop the third character
>>> target.pop(2)
'C'
>>> target
['A', 'B', 'D', 'E', 'F', 'G']
>>> #Pop trailing character
>>> target.pop()
'G'
>>> target
['A', 'B', 'D', 'E', 'F']
>>> #Pop the first character
>>> target.pop(0)
'A'
>>> target
['B', 'D', 'E', 'F']
>>> # extend()Combine list by
>>> target1 = ['A','B','C']
>>> target2 = ['D','E']
>>> target1.extend(target2)
>>> target1
['A', 'B', 'C', 'D', 'E']
>>> # +=Combine list by
>>> target1 = ['A','B','C']
>>> target2 = ['D','E']
>>> target1 += target2
>>> target1
['A', 'B', 'C', 'D', 'E']
>>> target = ['A','B','C','D','E','F','G']
>>> target.index('D')
3
>>> target = ['A','B','C','D','E','F','G']
>>> 'A' in target
True
>>> 'Z' in target
False
>>> target = ['A','A','A','B','B','C']
>>> target.count('A')
3
>>> target.count('B')
2
>>> target.count('C')
1
join () is a string method, not a list method.
>>> target = ['A','B','C','D','E','F','G']
>>> ','.join(target)
'A,B,C,D,E,F,G'
>>> target = ['A','B','C','D','E','F','G']
>>> len(target)
7
>>> #General function sorted()Ascending sort by
>>> target = ['C','E','A','D','B','F']
>>> sorted_target = sorted(target)
>>> sorted_target
['A', 'B', 'C', 'D', 'E', 'F']
>>> #General function sorted()Descending sort by
>>> target = ['C','E','A','D','B','F']
>>> sorted_target = sorted(target, reverse=True)
>>> sorted_target
['F', 'E', 'D', 'C', 'B', 'A']
>>> #List function sort()Ascending sort by
>>> target = ['C','E','A','D','B','F']
>>> target.sort()
>>> target
['A', 'B', 'C', 'D', 'E', 'F']
>>> #List function sort()Descending sort by
>>> target = ['C','E','A','D','B','F']
>>> target.sort(reverse=True)
>>> target
['F', 'E', 'D', 'C', 'B', 'A']
>>> target = ['A','B','C']
>>> #List copy()Copy by function
>>> target1 = target.copy()
>>> target1
['A', 'B', 'C']
>>> # list()Copy by conversion function
>>> target2 = list(target)
>>> target2
['A', 'B', 'C']
>>> #Copy by list slice
>>> target3 = target[:]
>>> target3
['A', 'B', 'C']
Unlike lists, tuples are immutable sequences, so you cannot add, remove, or change elements after defining a tuple. Also, since there are no functions such as append () and insert (), there are fewer functions than lists.
As a way of using properly, use tuples when dealing with constant lists. Other points to consider are as follows.
--Tuples consume less space. --There is no risk of rewriting tuple elements. --Tuples can be used as dictionary keys. --Named tuples can be used as a simple alternative to objects. --Function arguments are passed as tuples.
>>> #Creating an empty tuple
>>> empty_tuple = ()
>>> empty_tuple
()
>>> #Tuple with one element (with a comma at the end)
>>> target1 = 'A',
>>> target1
('A',)
>>> #Tuples with multiple elements (comma at the end can be omitted)
>>> target2 = 'A','B','C'
>>> target2
('A', 'B', 'C')
>>> #It is easier to understand if this is a tuple because it does not cause an error even if you put parentheses
>>> target3 = ('A','B','C')
>>> target3
('A', 'B', 'C')
>>> str_list = ['A','B','C']
>>> tuple(str_list)
('A', 'B', 'C')
>>> #List unpacked assignment
>>> str_list = ['A','B','C']
>>> s1, s2, s3 = str_list
>>> s1
'A'
>>> s2
'B'
>>> s3
'C'
>>> #Tuple unpacked substitution
>>> str_tuple = ('A','B','C')
>>> s1, s2, s3 = str_tuple
>>> s1
'A'
>>> s2
'B'
>>> s3
'C'
Recommended Posts