Python初始化多维数组

redswallow posted @ Sat, 10 Jul 2010 04:07:52 +0800 in python , 2282 readers

Python中初始化一个5 x 3每项为0的数组,最好方法是:

multilist = [[0 for col in range(5)] for row in range(3)]

我们知道,为了初始化一个一维数组,我们可以这样做:


alist = [0] * 5

没错,那我们初始化一个二维数组时,是否可以这样做呢:


multi = [[0] * 5* 3

其实,这样做是不对的,因为[0] * 5是一个一维数组的对象,* 3的话只是把对象的引用复制了3次,比如,我修改multi[0][0]:


multi = [[0] * 5* 3
multi[0][0] 
= 'Love China'
print multi

输出的结果将是:


[['Love China', 0, 0, 0, 0], ['Love China', 0, 0, 0, 0], ['Love China', 0, 0, 0, 0]]


我们修改了multi[0][0],却把我们的multi[1][0],multi[2][0]也修改了。这不是我们想要的结果。



如果我们这样写呢:


multilist = [[0] * 5 for row in range(3)]
multilist[0][0] 
= 'Love China'
print multilist

我们看输出结果:


[['Love China', 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]


恩,没问题。但是,由于使用 * 的方法比较容易引起混淆导致Bug,所以还是推荐使用上面第一种方法,即:


multilist = [[0 for col in range(5)] for row in range(3)]

 

Avatar_small
Landon Jackson said:
Mon, 21 Jan 2019 19:03:23 +0800

Really good write-up, Appreciate it intended for giving That expertise. Excellently published document, but only if many blog writers made available identical higher level of information since you, the online world is a significantly better area. You need to thanks! Tax Advice London

Avatar_small
beautiful bangles said:
Sun, 23 Jun 2019 20:22:30 +0800

I've been looking for info on this topic for a while. I'm happy this one is so great. Keep up the excellent work

Avatar_small
forgiveness, said:
Sun, 30 Jun 2019 14:04:06 +0800

I've been looking for info on this topic for a while. I'm happy this one is so great. Keep up the excellent work

Avatar_small
Instagram direct mes said:
Thu, 04 Jul 2019 11:03:09 +0800 Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.

 

Avatar_small
teleserye replay said:
Thu, 04 Jul 2019 19:55:59 +0800

<!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--> I've been looking for info on this topic for a while. I'm happy this one is so great. Keep up the excellent work


Avatar_small
walmartone said:
Fri, 06 Sep 2019 10:36:21 +0800 Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!

 

 

Avatar_small
free chat said:
Mon, 14 Oct 2019 12:07:41 +0800

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.

Avatar_small
http://www.tradwiki. said:
Fri, 18 Oct 2019 20:18:55 +0800

Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this.

Avatar_small
http://www.aquitaine said:
Fri, 25 Oct 2019 10:29:18 +0800

Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC.

Avatar_small
https://www.jackiecu said:
Sat, 02 Nov 2019 22:20:21 +0800

Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC.

Avatar_small
http://wroclaw.dlast said:
Thu, 07 Nov 2019 08:38:29 +0800

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.

Avatar_small
http://www.otonysa.p said:
Wed, 13 Nov 2019 07:47:12 +0800

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject.

Avatar_small
https://www.bdjscres said:
Thu, 14 Nov 2019 13:45:05 +0800

Junior School Certificate (JSC) examinations are recently wrapped up in the Bangladesh state along with the JDC exams. The exams are held for class 8 students for lakhs of students every year. This year over 30 lakh students are expected to have written the JSC exams at various examination centers of various division.

Avatar_small
http://admi.net/new/ said:
Mon, 18 Nov 2019 21:58:13 +0800

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.

Avatar_small
123mvoies said:
Thu, 28 May 2020 16:55:06 +0800

beautiful movie site https://123movies7.com

Avatar_small
sexting chat rooms said:
Wed, 10 Jun 2020 08:26:57 +0800

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work

Avatar_small
online sexting said:
Mon, 15 Jun 2020 00:20:30 +0800

I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job !

Avatar_small
Immobilienmakler Nür said:
Thu, 27 Aug 2020 02:17:18 +0800

Great blog! I am loving it!! Will come back again. I am bookmarking your feeds also

Avatar_small
sink splashback said:
Sat, 05 Sep 2020 20:40:31 +0800

This is very useful post for me. This will absolutely going to help me in my project.

Avatar_small
vaobong.biz said:
Sun, 06 Sep 2020 00:14:56 +0800

Most of these tournaments are done over the Thanksgiving Holiday and your team plays 2-3 games. Your football team is put in a bracket based on pre-set age and sometimes weight restrictions. Most tournaments have gone to unlimited weight with running back weights, with a few still having some total weight restricted divisions. The tournament host then puts teams into brackets based on record, perceived strength, historical league strength, coach preference, average age and sometimes average weight is considered as well. Most tournaments have very heavy restrictions as to verification of birth certificates and roster (no all-star teams) and most do weigh-ins the day of the games.

Avatar_small
sv388.com said:
Mon, 07 Sep 2020 19:04:53 +0800

The currency that the deposited valuables could support could be a maximum of seven times the value of the valuables on deposit.

Avatar_small
https://directoryofa said:
Mon, 07 Sep 2020 20:17:47 +0800

This time we are reviewing Blackbeards Adult Resort, we haven't been there in a few years and believe me it is better. I know, we have it real rough here at Resort Holiday Weeks, going around visiting resorts all over the world and having resort owners and staff treat us like royalty so that we give them a good review.

Avatar_small
Fence Post Driver said:
Sun, 13 Sep 2020 18:59:16 +0800

Thank You For This Post, was added to my bookmarks.

Avatar_small
immobilien essen said:
Wed, 30 Sep 2020 19:55:14 +0800

You certainly understand what you’re referring to,this blog is simply excellent .

Avatar_small
pak24tv said:
Mon, 05 Oct 2020 20:23:05 +0800

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. how to stop hair loss how to make money online how to win friends and influence people

Avatar_small
myra showroom said:
Tue, 06 Oct 2020 00:19:04 +0800

This could be the right weblog for everyone who desires to discover this topic. You already know a whole lot its almost hard to argue along (not too I just would want…HaHa). You actually put a fresh spin for a topic thats been discussed for several years. Wonderful stuff, just fantastic!

Avatar_small
RajaQQ said:
Thu, 22 Oct 2020 17:01:32 +0800

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read !! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.

Avatar_small
escort said:
Sat, 24 Oct 2020 01:02:52 +0800

Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter