A brief summary of Python collections

collection

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

list

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.

In-place operation

The operation that changes the value of the list is called in-place operation.

.sort () method

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]

Main methods of list

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

del statement

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

Tuple

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.

How to use list and tuple properly

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

dictionary

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

Set

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

Frozen Set

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

Set operations using sets

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

Handling of character strings

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

A brief summary of Python collections
A brief summary of Linux
A brief summary of qubits (beginners)
A brief summary of Pinax overview #djangoja
Summary of Python arguments
A brief summary of Graphviz in python (explained only for mac)
Summary of python file operations
Summary of Python3 list operations
A brief summary of Linux antivirus software for individuals
A record of patching a python package
Python Summary
A good description of Python decorators
[Python] A memorandum of beautiful soup4
Python summary
A rough summary of OS history
A Tour of Go Learning Summary
A beginner's summary of Python machine learning is super concise.
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
Here's a brief summary of how to get started with Django
[Python] Summary of how to use pandas
Display a list of alphabets in Python 3
Make a relation diagram of Python module
Summary of various for statements in Python
[Python] Summary of array generation (initialization) time! !! !!
Connect a lot of Python or and and
[Python2.7] Summary of how to use unittest
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Summary of built-in methods in Python list
Summary of useful techniques for Python Scrapy
Summary of how to use Python list
A brief description of pandas (Cheat Sheet)
[Python2.7] Summary of how to use subprocess
The story of blackjack A processing (python)
Axis option specification summary of Python "numpy.sum (...)"
[Python] Get a list of folders only
A memorandum of python string deletion process
Introduction of Python
Python tutorial summary
Basics of Python ①
Basics of python ①
A summary of Python e-books that are useful for free-to-read data analysis
Copy of python
python related summary
Python collections module
Python basics summary
Introduction of Python
A memo of a tutorial on running python on heroku
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Draw a graph of a quadratic function in Python
Correspondence summary of array operation of ruby and python
Summary of the differences between PHP and Python
Summary of how to import files in Python 3
[python] Create a list of various character types
Get the caller of a function in Python
A memorandum of calling Python from Common Lisp
Make a copy of the list in Python
A memorandum of extraction by python bs4 request
Summary of how to use MNIST in Python
Solve A ~ D of yuki coder 247 with python