Python3---数据类型---列表
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
Python有6个序列的内置类型,但最常见的是列表和元组。
序列都可以进行的操作包括索引,切片,加,乘,检查成员。
此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。
0X01;定义:
在Python中,用方括号表示一个list:[]
方括号里面的元素类型,可以是int,也可以是str类型的数据,甚至也能够是True/False这种布尔值
注意:
如果你已经了解了别的语言,比如比较常见的Java,里面有一个跟list相似的数据类型——数组——但是两者还是有区别的。在Java中,数组中的元素必须是基本数据类型中的某一个,也就是要么都是
int类型,要么都是char类型等,不能一个数组中既有int类型又有char类型。这是因为Java中的数组需要提前声明,声明的时候就确定了里面元素的类型。但是尽管Python中的list与Java中的数组有类似的地方
——都是[]包裹的——list中的元素是任意类型的,可以是int、str,还可以是list,甚至是dict等。所以,有一句话说:列表是Python中的苦力,什么都可以干。
0X02;反转
简单说就是将列表反转过来。举例说明:
alst = [1,2,3,4,5,6,7] print(alst[::-1])
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py [7, 6, 5, 4, 3, 2, 1] Process finished with exit code 0
0X03;列表的基本操作
len() 返回列表的长度,"+"连接两个序列,“*” 重复元素,“in” 查询列表list是否存在某个值,“max()”/"min()"返回最大值或最小值,比较列表,追加元素
1;追加元素list.append(x)
alst = [1,2,3,4,5,6,7] alst.append(8) print(alst)
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py [1, 2, 3, 4, 5, 6, 7, 8] Process finished with exit code 0
0X04;列表的函数
1;查看列表当中的函数:
alst = [1,2,3,4,5,6,7] print(dir(alst))
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__',
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Process finished with exit code 0
2;目前我们不去讨论类似“__xx__”的函数。主要查看'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
2.1;append()/extend()
查看函数append的使用方法
alst = [1,2,3,4,5,6,7]
help(alst.append)
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py Help on built-in function append: append(object, /) method of builtins.list instance Append object to the end of the list. Process finished with exit code 0
文档描述函数append功能是将对象放入指定列表最后的位置中。举例:
alst = [1,2,3,4,5,6,7] alst2 = [8,9,10] alst.append(alst2) alst2.append(9) print(alst) print(alst2)
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py [1, 2, 3, 4, 5, 6, 7, [8, 9, 10, 9]] [8, 9, 10, 9] Process finished with exit code 0
函数extend()
查看用法:
alst = [1,2,3,4,5,6,7] alst2 = [8,9,10] help(alst.extend)
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py Help on built-in function extend: extend(iterable, /) method of builtins.list instance Extend list by appending elements from the iterable. Process finished with exit code 0
举例:
alst = [1,2,3,4,5,6,7] alst2 = [8,9,10] alst.extend(alst2) print(alst)
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Process finished with exit code 0
概括起来,extend函数也是将另外的元素增加到一个已知列表中,元素必须是iterable,某些类型的对象是“可迭代”(iterable)的,这里用内建函数hasattr()判断一个字符串是否是可迭代的
list经过extend()方法之后比原来扩容了,但是,并没有离开原来的“窝”,即在内存中还是“旧”的,只不过里面的内容增多了。
append是整建制地追加,extend是个体化扩编。
2.2;count的作用是数一数某个元素在该list中出现多少次,也就是某个元素有多少个。
2.3;index() Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。
2.4;list.insert(i,x)就是向列表插入元素的函数,list.insert(i,x)中的i是将元素x插入到列表中的位置,即将x插入到索引值是i的元素前面。注意,索引是从0开始的
2.5;list.remove(x) 和list.pop 删除列表元素
2.6;reverse比较简单,就是把列表的元素顺序反过来。
2.7;list.sort()也是让列表进行原地修改,没有返回值。默认情况如上面的操作,实现的是从小到大的排序。
什么是保留字?在Python中,某些词语或者拼写是不能被用户拿来做变量、函数、类等命名,因为它们已经被语言本身先占用了。这些就是所谓的保留字。在Python中,以下是保留字,不能用于变成任何命名。
and,assert,break,class,continue,def,del,elif,else,except,exec,finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,return,try,while,with,yield
这些保留字都是我们在编程中要用到的。
举例:
1 #创建一个列表 2 new_list = ['a','b','c','d'] #与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。 3 4 #访问列表中的值 5 print("访问列表") 6 a = new_list[3] 7 b = new_list[1:3] 8 print(a,b) 9 10 print("----------------------------------") 11 print("更新列表") 12 #更新列表 13 #append()函数来添加元素 14 new_list.append('e') 15 for i in new_list: 16 print("新增后列表打印:",i) 17 18 print("----------------------------------") 19 print("删除列表") 20 #删除列表 21 #使用del语句来删除列表中的元素 22 del new_list[1] 23 for i in new_list: 24 print("删除后列表打印:",i) 25 26 print("----------------------------------") 27 print("列表截取") 28 c = new_list[2] #截取从左到右读取第三个元素 29 d = new_list[-2] #截取从右到左读取第二个元素 30 e = new_list[1:] #截取第二个元素之后的列表 31 print("截取从左到右读取第三个元素:",c) 32 print("截取从右到左读取第二个元素",d) 33 print("截取第二个元素之后的列表",e) 34 35 print("----------------------------------")
运行结果:
1 C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py 2 访问列表 3 d ['b', 'c'] 4 ---------------------------------- 5 更新列表 6 新增后列表打印: a 7 新增后列表打印: b 8 新增后列表打印: c 9 新增后列表打印: d 10 新增后列表打印: e 11 ---------------------------------- 12 删除列表 13 删除后列表打印: a 14 删除后列表打印: c 15 删除后列表打印: d 16 删除后列表打印: e 17 ---------------------------------- 18 列表截取 19 截取从左到右读取第三个元素: d 20 截取从右到左读取第二个元素 d 21 截取第二个元素之后的列表 ['c', 'd', 'e'] 22 ---------------------------------- 23 24 Process finished with exit code 0