Python初始化多维数组
redswallow
posted @ Sat, 10 Jul 2010 04:07:52 +0800
in python
, 2374 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
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
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)]
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
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
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
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.
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
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!
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.
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.
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.
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.
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.
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.
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.
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.
Thu, 28 May 2020 16:55:06 +0800
beautiful movie site https://123movies7.com
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
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 !
Thu, 27 Aug 2020 02:17:18 +0800
Great blog! I am loving it!! Will come back again. I am bookmarking your feeds also
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.
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.
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.
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.
Sun, 13 Sep 2020 18:59:16 +0800
Thank You For This Post, was added to my bookmarks.
Wed, 30 Sep 2020 19:55:14 +0800
You certainly understand what you’re referring to,this blog is simply excellent .
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
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!
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.
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.
Sun, 25 Oct 2020 03:23:03 +0800
The worst part of it was that the software only worked intermittently and the data was not accurate. You obviously canot confront anyone about what you have discovered if the information is not right.
Fri, 30 Oct 2020 20:49:40 +0800
Sarongs Dashiki Shirt Store for sale provides hundreds of Color, sarong skirt , sarong beach , beach wrap skirt to ship worldwide.
Wed, 04 Nov 2020 19:25:22 +0800
Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine.
Sat, 07 Nov 2020 22:12:30 +0800
Writing with style and getting good compliments on the article is quite hard, to be honest.But you've done it so calmly and with so cool feeling and you've nailed the job. This article is possessed with style and I am giving good compliment. Best!
Tue, 24 Nov 2020 02:23:25 +0800
I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
Fri, 27 Nov 2020 01:01:31 +0800
It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.
Sat, 05 Dec 2020 20:01:46 +0800
I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
Sat, 05 Dec 2020 22:41:49 +0800
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
Sat, 12 Dec 2020 01:54:41 +0800
You have done a amazing job with you website
Sun, 20 Dec 2020 22:37:29 +0800
This is an awesome motivating article.I am practically satisfied with your great work.You put truly extremely supportive data. Keep it up. Continue blogging. Hoping to perusing your next post
Mon, 21 Dec 2020 18:32:11 +0800
It’s a pity you don’t have a donate button! I’d most certainly donate to this fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this site with my Facebook group. Talk soon! software service desk
Tue, 22 Dec 2020 22:04:47 +0800
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
Wed, 06 Jan 2021 22:29:35 +0800
I curious more interest in some of them hope you will give more information on this topics in your next articles.
Tue, 12 Jan 2021 22:20:15 +0800
Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!
Wed, 13 Jan 2021 22:35:28 +0800
It proved to be Very helpful to me and I am sure to all the commentators here!
Thu, 14 Jan 2021 19:15:04 +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
Sat, 16 Jan 2021 22:52:00 +0800
Cool stuff you have and you keep overhaul every one of us
Sun, 17 Jan 2021 21:05:28 +0800
A debt of gratitude is in order for sharing the information, keep doing awesome... I truly delighted in investigating your site. great asset...
Tue, 19 Jan 2021 22:24:07 +0800
I think this is a standout amongst the most critical data for me. What"s more, i"m happy perusing your article. Be that as it may, ought to comment on some broad things
Wed, 20 Jan 2021 19:02:33 +0800
Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.