Detailed introduction to Python array definition method
The examples in this article describe the Python array definition method. Share it with everyone for your reference, the details are as follows:
There is no data structure for arrays in Python, but lists are very similar to arrays, such as:
a=[0,1,2]
At this time: a[0]=0, a[1]=1, a[[2]=2, but it raises a question, that is, what if the array a wants to be defined as 0 to 999? This may be Implemented through a = range(0, 1000). Or omit it as a = range(1000). If you want to define a with a length of 1000, and the initial values are all 0, then a = [0 for x in range(0, 1000)]
The following is a two-dimensional array Definition:
Direct definition:
a=[[1,1],[1,1]]
This defines a 2*2 two-dimensional array with an initial value of 0.
Indirect definition:
a=[[0 for x in range(10)] for y in range(10)]
This defines a 10*10 two-dimensional array with an initial value of 0.
There is also a simpler method of literal two-dimensional array:
b = [[0]*10]*10
Define a 10*10 two-dimensional array with an initial value of 0.
Compare with a=[[0 for x in range(10)] for y in range(10)]: the result of print a==b is True.
But after using the definition method of b instead of a, the program that could run normally before also went wrong. After careful analysis, the difference was found:
a[0][0]=1 , only a[0][0] is 1, and the others are all 0.
When b[0][0]=1, a[0][0], a[1][0], and a[9,0] are all 1.
From this we get that the 10 small one-dimensional data in the large array all have the same reference, that is, they point to the same address.
So b = [[0]*10]*10 does not conform to our conventional two-dimensional array.
After testing at the same time: the definition of c=[0]*10 has the same effect as c=[0 for x in range(10)], without the problem of the same reference above, it is estimated that the definition of array c At this time, the value type is multiplied, while in the previous b, the type is multiplied, because the one-dimensional array is a reference (borrowing the value type and reference type in C#, I don’t know if it is appropriate).
For more detailed introduction to Python array definition methods and related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
