--A data type with many elements. It is used when combining multiple values. --There are two types. --Sequence type: Elements are ordered and accessible in order --Some can change the order and some do not. --Mapping type: meaningless in order
There is a turn | No order | |
---|---|---|
Can be changed | list | Dictionary, set(Set) |
Unchangeable | Tuples, strings | set(Frozen Set) |
Modifiable sequence type. Describe by enclosing it in square brackets []
. It can contain different elements.
>>> squares = [1, 4, 9, 16, 25, 36]
You can also specify an index to retrieve a specific value.
value | 1 | 4 | 9 | 16 | 25 | 36 |
---|---|---|---|---|---|---|
index | 0 | 1 | 2 | 3 | 4 | 5 |
index(-) | -6 | -5 | -4 | -3 | -2 | -1 |
>>> squares = [1, 4, 9, 16, 25, 36]
>>> squares[2]
9
>>> squares[-4]
9
You can use slice
([start point: end point-1]) to retrieve a range of values.
>>> squares[2:5] #Output the 3rd to 5th values
[9, 16, 25]
>>> squares[-3:] #Outputs the values from the third to the last
[16, 25, 36]
You can also change the value by specifying the position of the value.
>>> squares[4] = 30
>>> squares[-5] = 7
>>> squares
[1, 7, 9, 16, 30, 36]
You can also change the value in slices.
>>> squares[2:5] = [10,20,35]
>>> squares
[1, 7, 10, 20, 35, 36]
Character strings can also be included in the list. What you can do is the same as the numerical value.
The operation that changes the value of the list is called in-place operation
.
Sort the values in the list. Similar to the sorted () function, except that the contents of the list are rewritten.
sorted()function
>>> list_a = [1, 4, 3, 5, 2]
>>> sorted(list_a) #Sorting values
[1, 2, 3, 4, 5]
>>> list_a #The contents of the list cannot be rewritten
[1, 4, 3, 5, 2]
>>> list_a = sorted(list_a) #If you want to rewrite the contents of the list, use this form
>>> list_a
[1, 2, 3, 4, 5]
With the .sort () method
.sort()Method
>>> list_a = [1, 4, 3, 5, 2]
>>> list_a.sort()
>>> list_a
[1, 2, 3, 4, 5]
Method | Operation details |
---|---|
list.append(x) | Add an element to the end of the list |
list.extend(iterable) | Add all elements of an iterator to the end |
list.insert(i, x) | Add element x at the position specified by index i |
list.remove(x) | If there is a value equal to x in the list, delete the first element. ValueError if there is no equal value |
list.pop() | Removes the end of the list and returns its elements. Specifying an index in parentheses removes a particular element and returns that element. |
list.clear() | Delete all elements in the list |
list.index(x) | If there is a value equal to x in the list, the index of that element is returned. ValueError if there is no equal value |
list.count(x) | Returns the number of x in the list |
list.reverse() | Reverse the elements of the list |
.Remov () and .pop () are not the only ways to remove list elements. It can also be deleted with the del statement
.
>>> list = [1, 2, 3, 4, 5]
>>> del list[2] #Specify an index to delete a specific element
>>> list
[1, 2, 4, 5]
>>> del list[1:3] #Use slices to remove a range of elements
>>> list
[1, 5]
Immutable sequence type. Describe in parentheses
.
>>> numbers = (1, 2, 3, 4, 5, 6)
What you can do with indexes and slices is the same as lists.
Tuple if you don't want to change the value.
--Settings in the application --Retaining CSV record data --Card representation of playing cards ('Spade', '1')
Such
An unordered (index cannot be used) type that is managed by a combination of key and value. Enclose these two in curly braces
and describe them.
You can retrieve the value by specifying the key. You can also change the value by specifying the key.
>>> dacho = {'higo':'leader', 'jimon':'muscle', 'ueshima':'kiss'}
>>> dacho['ueshima']
'kiss'
>>> dacho['ueshima'] = 'kururimpa'
>>> dacho['ueshima']
'kururimpa'
Use the .values () method
if you want only the values, or the .items () method
if you want the key and value as a set.
>>> dacho = {'higo':'leader', 'jimon':'muscle', 'ueshima':'kiss'}
>>> for d in dacho.values():
... print(d)
...
leader
muscle
kiss
>>> for d in dacho.items():
... print(d)
...
('higo', 'leader')
('jimon', 'muscle')
('ueshima', 'kiss')
A type that has no compounding elements and no order (indexes cannot be used). Write in curly braces
. If the elements are duplicated, they are combined into one. `Variable. ``
>>> hollywood = {'h', 'o', 'l', 'l', 'y', 'w', 'o', 'o', 'd'}
>>> hollywood
{'o', 'w', 'h', 'y', 'd', 'l'}
A type that has no compounding elements and no order (indexes cannot be used). Write in curly braces
. If the elements are duplicated, they are combined into one. `Invariant. ``
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a | b #sum(Included in a or b)
{1, 2, 3, 4}
>>> a.union(b)
{1, 2, 3, 4}
>>> a & b #product(Common to a and b)
{2, 3}
>>> a.intersection(b)
{2, 3}
>>> a - b #difference(Included only in a)
{1}
>>> a.difference(b)
{1}
>>> a ^ b #Symmetric difference(Included only in a or b)
{1, 4}
>>> a.symmetric_difference(b)
{1, 4}
You can assign the result of an operation to a variable by adding _update
after the method.
The string is treated as an immutable iterator with elements. It is also possible to take out a certain range using slices.
>>> str = 'abcdefg'
>>> str[2:5]
'cde'
Recommended Posts