python 基础数据类型-组的概念

时间:2020-07-30 01:19:50   收藏:0   阅读:88
python 的组也就是list 是这样的
>>> type([1,2,3,4,5,6]) <class list>
跟Java不同的是,
(1)Java这种叫数组,python 叫list。
(2)Java一个数组,存的都是相同类型,python list 里 可以是不同的,比如字符串、整型、布尔、甚至是嵌套的数组(这个我觉得也可以叫二维数组),都是可以的
type(["1",1,"hello",[1,2,3]]) <class list>

 

取list 可以按下表操作,包括操作二维数组
>>> list[0] 1 >>> list[2] hello >>> list[3] [1, 2, 3] >>> >>> list[3][1] 2 >>> list[3][3] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> >>> list[3][2] 3

>>> list[2:]
[‘hello‘, [1, 2, 3]]
>>> list[-2:]
[‘hello‘, [1, 2, 3]]
>>> list[-2:3]
[‘hello‘]



操作:两个列表相加
>>> list1 = [1,2,2] >>> list+list1 [1, 1, hello, [1, 2, 3], 1, 2, 2]

 

对比一下,其实string 类型也有类似组的一些操作,通过有序下表,取出每一个字符(通过下表,取出组里指定的元素)
>>> "helllo world"[0] h >>> >>> >>> 111[0] <stdin>:1: SyntaxWarning: int object is not subscriptable; perhaps you missed a comma? Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: int object is not subscriptable

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!