python 基础数据类型-列表的概念

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 类型也有类似组的一些操作,通过有序下表,取出每一个字符(通过下表,取出组里指定的元素)
对比下来,int float 都没有这种操作的
>>> "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
list(也叫组,也叫序列,序列是比较专业的名词)的切片操作(顾名思义,切一个片段出来)
>>> [1,2,3][2] 3 >>> [1,2,3][1] 2 >>> >>> >>> [1,2,3,4,5][1:3] [2, 3] >>> [1,2,3,4,5][-1:3] [] >>> [1,2,3,4,5][-3:3] [3]
三个元素的切片[起始下表:终止下表(开区间):步长],至少Matlab管这个叫
步长
[1,2,3,4,5,6,7,8,9][0:8:2] [1, 3, 5, 7]
最大值最小值计算:
一维list,str,boolean
二维list
>>> min([1,2,3]) 1 >>> max([1,2,3]) 3 >>> (min([[1,2,3],[4,5,6]])) [1, 2, 3] >>> min(min([[1,2,3],[4,5,6]])) 1 >>> max([[1,2,3],[4,5,6]]) [4, 5, 6] >>> max(max([[1,2,3],[4,5,6]])) 6 >>> >>> >>> max(min([[1,2,3],[4,5,6]])) 3 >>> >>> >>> max("abc") 'c' >>> max(["a","b","c"]) 'c' >>> >>> >>> >>> max([True,False]) True

猜你喜欢

转载自www.cnblogs.com/ansonwan/p/13401462.html