[Python] Chapter 04-04 Various data structures (see list)

[Python] Chapter 04-04 Reference List

Here, I would like to talk about "references" in the sense that I want you to know the characteristics of the list, rather than explaining new knowledge.

Object reference

First, let's check the reference. Enter the following code in the ** Python Console **.

>>>Lx = [8, 6, 5, 9, 7]
>>>Ly = Lx
>>>Ly
[8, 6, 5, 9, 7]

In this state, enter the following code.

>>>Ly[4] = 1000
>>>Ly
[8, 6, 5, 9, 1000]

I think you can understand up to this point. Briefly, assign the list [8, 6, 5, 9, 7] to the variable ** Lx **, assign it to Ly, then assign 1000 to Ly [4] to get the contents. Confirming. Of course, if you display Ly, it will be [8, 6, 5, 9, ** 1000 **].

Now, let's display ** Lx ** in this state. Then it will be as follows.

>>>Lx
[8, 6, 5, 9, 1000]

You should have changed the elements of the ** Ly ** list earlier, but you can see that ** Lx ** has also changed.

I would like to take a look behind the scenes.

First,

>>>Lx = [8, 6, 5, 9, 7]

Regarding, I am assigning a list to Lx. The important thing here is [Chapter 02 "Variables"](https://qiita.com/ko0821/items/8355b4e192aa76a0e8ae#%E5%A4%89%E6%95%B0%E9%87%8D%E8% As I explained at the time of A6% 81), I mentioned that ** variables are tags **.

Again, tagging [8, 6, 5, 9, 7] in the list with ** Lx ** is the same as explained in Chapter 02. The figure is as follows.

image.png

Then next

>>>Ly = Lx
>>>Ly
[8, 6, 5, 9, 7]

Let's take a look at. Here, regarding ** Ly = Lx **, when this assignment is made, it means that ** see what the variable Lx refers to as well as the variable Ly **.

Then, I think it can be represented in the figure below.

image.png

** Lx ** and ** Ly ** are ** referencing the same list **. Therefore, when Ly is output, [8, 6, 5, 9, 7] is output.

Then in that state,

>>>Ly[4] = 1000
>>>Ly
[8, 6, 5, 9, 1000]

This section explains.

Since the entity that Ly refers to is [8, 6, 5, 9, 7], if Ly [4] = 1000, the operation is for the entity [8, 6, 5, 9, 7]. , [8, 6, 5, 9, ** 1000 **].

Therefore, it can be represented by the following figure.

image.png

In such a state, if you enter ** Lx **,

>>>Lx
[8, 6, 5, 9, 1000]

And since the entity that refers to ** Ly ** is the same as ** Lx **, the output result of ** Lx ** will also change.

Not limited to the references in this list, references will appear in various places in the future. It is commonly referred to as ** object reference **.

(*) </ font> Those who have mastered the Java language may be familiar with this concept of reference. I think the same thing happened in "array".

About expressions for future variables

So far, we have explained with "assignment" such as "assigning a value to a variable called ** x **". From now on, we will use expressions such as "variable ** x ** refers to an entity (object)" and "variable ** x ** indicates".

Garbage collection

Until now, the variables ** Lx ** and ** Ly ** referred to the list (object), and by referring to it, the contents of the actual list could be confirmed.

Now, with the above situation in mind, enter the following code from the ** Python Console **.

>>>Lx = 10
>>>Ly = ['Japan', 'Canada']

What I want to say here is what happens to the actual list [8, 6, 5, 9, 1000].

Since the variables ** Lx ** and ** Ly ** refer to different entities, we cannot reach the list of entities [8, 6, 5, 9, 1000].

If this is left as it is, the memory of the computer will be full. To avoid this, Python determines that it is "garbage data" and deletes the actual list [8, 6, 5, 9, 1000].

This is called ** garbage collection **.

Finally

This time I have described the features of the list. To be honest, I don't use "reference" in practice, but it's something that can be found in any Python book.

Those who are thinking of choosing Python for the Fundamental Information Technology Engineer Examination are likely to be asked in the future, so be sure to remember it.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 04-04 Various data structures (see list)
[Python] Chapter 04-03 Various data structures (multidimensional list)
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-06 Various data structures (creating dictionaries)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python for Data Analysis Chapter 4
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3
Python data structures learned with chemoinformatics
[Python] list
Python Application: Data Visualization Part 3: Various Graphs
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
Python data structure and internal implementation ~ List ~
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Python] Various data processing using Numpy arrays
Python basics: list
[python] Create a list of various character types
# 3 [python3] Various operators
Python> Comprehension / Comprehension> List comprehension
Python Application: Data Handling Part 2: Parsing Various Data Formats
Python list manipulation
[python] Read data
List of Python libraries for data scientists and data engineers
List of Python code used in big data analysis
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Sorted list in Python
Python Exercise 2 --List Comprehension
Data analysis with python 2
List of python modules
Python> list> extend () or + =
Python Data Visualization Libraries
Data analysis using Python 0
Data analysis overview python
Python list comprehension speed
Filter List in Python
python unittest assertXXX list
Various Python visualization tools
Data cleaning using Python
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
[Python] Chapter 01-01 About Python (First Python)
Data analysis with Python
Python exception class list
Various processing of Python
Initialize list with python
[Python] It might be useful to list the data frames
Various ways to calculate the similarity between data in python