Python creates a two-dimensional array instance (a small pitfall on list), and python two-dimensional array

Source: Internet
Author: User

Python creates a two-dimensional array instance (a small pitfall on list), and python two-dimensional array

0. Directory

1. Problems Encountered

2. How to create a two-dimensional array

• 3.1 Direct Creation Method

• 3.2 List generative Method

• 3.3 create with module numpy

1. Problems Encountered

Today, when I was writing Python code, I encountered a big pitfall, which almost delayed my assignment...

The problem is that I need to create a two-dimensional array, as shown below:

m = n = 3test = [[0] * m] * nprint("test =", test)

The output result is as follows:

test = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Does it look okay?

At the beginning, I thought so too. I thought that I used the wrong function in other places. The result was as follows:

m = n = 3test = [[0] * m] * nprint("test =", test) test[0][0] = 233print("test =", test)

The output result is as follows:

test = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]test = [[233, 0, 0], [233, 0, 0], [233, 0, 0]]

Are you surprised ?!

This problem really happened to me at noon. I went to the Internet to search for it. The instructions in the official documents are as follows:

Note also that the copies are shallow; nested structures are not copied. This often haunts new Python programmers; consider:

>>> lists = [[]] * 3>>> lists[[], [], []]>>> lists[0].append(3)>>> lists[[3], [3], [3]]

What has happened is that [[[] is a one-element list containing an empty list, so all three elements of [[] * 3 are (pointers) this single empty list. modifying any of the elements of lists modifies this single list. you can create a list of different lists this way:

>>>>>> lists = [[] for i in range(3)]>>> lists[0].append(3)>>> lists[1].append(5)>>> lists[2].append(7)>>> lists[[3], [5], [7]]

In other words, in the matrix = [array] * 3 operation, only three references pointing to the array are created. Therefore, once the array changes, the three lists in the matrix also change.

2. How to create a two-dimensional array

2.1 Direct Creation Method

test = [0, 0, 0], [0, 0, 0], [0, 0, 0]]

Simple and rude, but too troublesome.

2.2 list Generation Method

test = [[0 for i in range(m)] for j in range(n)]

Learn how to use the list generative method to benefit the whole life. No, you can go to the list builder-liao Xuefeng's official website to learn.

2.3 create with module numpy

import numpy as nptest = np.zeros((m, n), dtype=np.int)

For more information about the module numpy. zeros, goHow to Use numpy. zeros (np. zeros) in pythonLook.

In the above example, Python creates a two-dimensional array instance (a small pitfall on list), which is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.