My error summary when I use Python to brush a question

Source: Internet
Author: User

I novice, in the Leetcode brush problem in the process of many problems, so this summary, not regularly updated.

1, in the creation of a two-dimensional list, I will use a = [[0] * 5] * 5, but this output will often be the same as expected, I always thought it was my program has a problem, Baidu for a long time also do not know where the error, and then see others the solution, You changed a code to create and initialize the list, and the result is run, and the error is:[[]] is a list containing an empty list element, so [[]]*3 represents 3 references to this empty list element, and modifying any one element changes the entire list . So the way I used to do this now is to use a list-generating formula:a = [[0] * 5 for _ in Xrange (5)] Link: http://www.cnblogs.com/btchenguang/archive/2012/01/30/23324 79.html

2, in fact, and 1, the B list is known, when I want to perform replication list B and modify the copied list A when (a = b) , if I change a after the value of B will also change, because they are now pointing to the same list, if you want to copy can be executed a = b[:], with a shard operation.

My error summary when I use Python to brush a question

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.