首页 > 编程语言 > 详细

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

时间:2020-07-30 09:41:30      阅读:75      评论:0      收藏:0      [点我收藏+]
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

 

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

原文:https://www.cnblogs.com/ansonwan/p/13401462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!