博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《python》---数组和多维数组的产生
阅读量:4230 次
发布时间:2019-05-26

本文共 720 字,大约阅读时间需要 2 分钟。

一维数组

定义以及初始化

>>> a = [1,2,3]>>> a[1, 2, 3]>>> 初始化一维数组>>> [0 for x in range(10)][0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

二维数组

>>> a = [[1,1],[2,2]]>>> a[[1, 1], [2, 2]]>>> myList = [[0]*3]*4[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]>>> myList[0][1] = 1>>> myList[[0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]]

发现当初始化myList[0][1]时候所有的元素中的第二个元素都改变,因为

list * n—>n shallow copies of list concatenated, n个list的浅拷贝的连接
当*3 只是对myList的第一个元素进行拷贝,全是一个相同的引用,即指向同一地址,当改变第一个元素时候,copy的其余的元素都要改变,所以第二列的元素都改变了。

则二维数组的产生代码为

>>> lists = [[ 0 for col in range(3)] for row in range(5)]>>> list[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]>>> lists[0][1] = 1>>> lists[[0, 1, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

使用random函数产生随机数组

参考博文

转载地址:http://eoiqi.baihongyu.com/

你可能感兴趣的文章
Beginning Java™ SE 6 Platform: From Novice to Professional
查看>>
Ethernet Networking for the Small Office and Professional Home Office
查看>>
Patterns for Computer-Mediated Interaction
查看>>
Mike Meyers' A+ Certification Passport, Third Edition
查看>>
The Rails Way
查看>>
MATLAB Demystified
查看>>
SharePoint 2007: The Definitive Guide [ILLUSTRATED]
查看>>
WCDMA for UMTS: HSPA Evolution and LTE
查看>>
Linux System Programming: Talking Directly to the Kernel and C Library [ILLUSTRATED]
查看>>
Prototype and script.aculo.us: You Never Knew JavaScript Could Do This!
查看>>
Beginning Joomla!: From Novice to Professional
查看>>
Understanding MySQL Internals
查看>>
Numerical Recipes 3rd Edition: The Art of Scientific Computing
查看>>
The Definitive Guide to the Microsoft Enterprise Library
查看>>
The Next Generation CDMA Technologies
查看>>
Photoshop CS3 Essential Skills
查看>>
Oracle Collaboration Suite Handbook
查看>>
ASP.NET AJAX Programmer's Reference: with ASP.NET 2.0 or ASP.NET 3.5
查看>>
Pro SharePoint 2007 Development Techniques
查看>>
CCNA: Cisco Certified Network Associate: Fast Pass
查看>>