"Go" How to initialize a two-dimensional an array in Python?

Source: Internet
Author: User

Tag: Object res element note DDR because pre lang [1]

"Wrong:"

m=[[element] * numcols] * numrows

For example:

>>> m=[[' A '] * 2
>>> m
[[' A ', ' a ', ' a '], [' A ', ' a ', ' a ']]
>>> M[1][1]
A
>>> m[1][1]= ' B '
>>> m
[[' A ', ' b', ' a '], [' A ', ' B ', ' a ']]

Note: At this time m[0][1] has also been modified, because is * copying the address of the object (list), m[0][1] and m[1][1] reference to same object.

"Right:"

m = [[element for x in range(numcols)] for y in range(numrows)]
for example:

>>> m=[[' A ' for X in range (3)] for Y in range (2)]
>>> m
[[' A ', ' a ', ' a '], [' A ', ' a ', ' a ']]
>>> M[1][1]
A
>>> m[1][1]= ' B '
>>> m
[[' A ', ' a ', ' a '], [' A ', 'b', ' a ']]

 

"Go" How to initialize a two-dimensional an array in Python?

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.