Python学习3——列表和元组
一、通用序列操作——索引、切片、相加、相乘、成员资格检查
1、索引,正序从0开始为第一个元素,逆序从-1开始,-1为最后一个元素
>>> greeting[0] 'h' >>> greeting[-1] 'l'
同时如果函数返回的是一个序列,可以直接对其进行索引操作
>>> fourth=input("year:")[3] year:2019 >>> fourth '9'
2、切片:使用索引来访问特定范围内元素,同时可指定步长,中间用冒号分开。切片区间左闭右开。
如果切片结束于最后一个元素,可省略第二个索引;如果切片起始于第一个元素,可省略第一个索引。如果要复制整个序列,两个索引都可省略。
>>> numbers=[0,1,2,3,4,5,6,7,8,9] >>> numbers[7:10] [7, 8, 9] >>> numbers[3:]#结束于最后一个元素 [3, 4, 5, 6, 7, 8, 9] >>> numbers[:3]#起始于第一个元素 [0, 1, 2] >>> numbers[:]#复制整个序列 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> numbers[:-3]#正数索引和负数索引可混用,此时不要求第一个索引数值小于第二个索引值 [0, 1, 2, 3, 4, 5, 6] >>> numbers[-3:0]#切片时,如果第一个索引对应的元素位于第二个索引对应的元素的后面,结果为空序列。 [] >>> numbers[-3:-1] [7, 8] >>> nmbers[-1:-3]#索引值统一时要求第一个索引值小于第二个索引值,这是因为步长的原因 Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> nmbers[-1:-3] NameError: name 'nmbers' is not defined >>> numbers[::2] [0, 2, 4, 6, 8] >>> numbers[0:10:-2] [] >>> numbers[10:0:-2] [9, 7, 5, 3, 1] >>> numbers[5::-2] [5, 3, 1] >>> numbers[:5:-2] [9, 7]
3、序列相加
直接使用 + 来拼接序列,但是一般来说不可以拼接不同类型的序列。
>>> [1,2,3]+[7,8,9] [1, 2, 3, 7, 8, 9] >>> [1,2,3]+"hello" Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> [1,2,3]+"hello" TypeError: can only concatenate list (not "str") to list
4、序列乘法
将序列与数x相乘。将重复这个序列x次来创建一个新序列。
>>> "hello"*5 'hellohellohellohellohello' >>> str="hello" >>> str*5 'hellohellohellohellohello' >>> str #不改变原序列,而是创建一个新序列 'hello' >>> [1,2,3]*5 [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> sequance=[None]*10 #用来创建一个长度为10的空序列 >>> sequance [None, None, None, None, None, None, None, None, None, None]
5、成员资格检查
使用 in运算符检查特定的值是否存在序列中,满足时返回True,不满足时返回False,这样的运算符称之为布尔运算符。
>>> Name=["jiangmeng","zhangsan","lisi"] >>> "jiameng" in Name True >>> "wanger"in Name False >>> name="lalallala" >>> "a" in name True >>> 'laa' in name #对于字符串来说,可以检查是否包含特定的字符串们可以用来过滤垃圾邮件 False
二、列表:Python的主力
列表与字符串和元组的关键区别——列表可变,字符串、元组不可变!
1、list () 函数(实际是一个类),有些情况下使用字符串来创建列表很有用,而且可以将任何序列作为list的参数。
>>> str="hello,python" >>> list1=[1,2,3] >>> tuple1=(1,2,3) >>> list(tuple1) [1, 2, 3] >>> list(list1) [1, 2, 3] >>> plist=list(str) >>> plist ['h', 'e', 'l', 'l', 'o', ',', 'p', 'y', 't', 'h', 'o', 'n'] >>> ''.join(plist)#将字符列表转换为字符串 'hello,python'
2、基本列表操作——元素赋值、删除元素、切片赋值、
>>> list_temp=[1,2,3,4,5,6] >>> list_temp[1]=1 >>> list_temp [1, 1, 3, 4, 5, 6] >>> del list_temp[1] >>> list_temp [1, 3, 4, 5, 6]
切片赋值——切片是一项极其强大的功能,而能够给切片赋值让这项功能更加强大
>>> name=list("jiameng") >>> name ['j', 'i', 'a', 'm', 'e', 'n', 'g'] >>> name[3:]=list('boy') >>> name ['j', 'i', 'a', 'b', 'o', 'y'] >>> name[0:0]=list("name:")#插入新元素 >>> name ['n', 'a', 'm', 'e', ':', 'j', 'i', 'a', 'b', 'o', 'y'] >>> name[1:10]=[]#替换空切片,类似del >>> name ['n', 'y'] #还可以步长不为1地进行切片赋值,但目前不知道问题出在哪里,没能解决这个问题
3、列表方法——append clear copy count extend index pop remove reverse sort
#append >>> list1=[1,2,3] >>> list1.append(4)#只允许有一个对象添加到列表末尾 >>> list1 [1, 2, 3, 4] #clear >>> list1.clear() >>> list1 [] #copy 完全复制出来一个副本,而不是将另一个名称list2关联到list1列表 >>> list1=[1,2,3] >>> list2=list1.copy() >>> list2[0]=0 >>> list1 [1, 2, 3] >>> list2 [0, 2, 3] #count 用来计算指定元素在列表中出现了几次 >>> list1=list("hello,python!I love you!") >>> list1 ['h', 'e', 'l', 'l', 'o', ',', 'p', 'y', 't', 'h', 'o', 'n', '!', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u', '!'] >>> list1.count('l') 3 >>> list1.count("ll") 0 #extend 使用一个列表来扩展另一个列表,区别于序列拼接,这里是修改被扩展序列,而不是产生一个新的序列 >>> a=list('jia') >>> b=list('meng') >>> a.extend(b) >>> a ['j', 'i', 'a', 'm', 'e', 'n', 'g'] >>> b ['m', 'e', 'n', 'g'] #index 在列表中查找出指定值第一次出现的索引 >>> a=list('hello,python') >>> a ['h', 'e', 'l', 'l', 'o', ',', 'p', 'y', 't', 'h', 'o', 'n'] >>> a.index('l') 2 >>> a.index('a') Traceback (most recent call last): File "<pyshell#114>", line 1, in <module> a.index('a') ValueError: 'a' is not in list #insert 将一个对象插入列表,有两个参数 >>> numbers=[1,2,3,5,6] >>> numbers.insert(3,4) >>> numbers [1, 2, 3, 4, 5, 6] #pop 删除一个元素,默认是最后一个元素,并返回这个元素,pop是唯一既修改列表有返回一个非None值的列表方法。Python不提供push,但可以使用append来代替 >>> numbers [1, 2, 3, 4, 5, 6] >>> numbers [1, 2, 3, 4, 5, 6] >>> numbers.pop() 6 >>> numbers [1, 2, 3, 4, 5] >>> numbers.pop(0) 1 >>> numbers [2, 3, 4, 5] #remove 用于删除第一个为指定值的元素 就地修改,不返回值 >>> characters=['hello','byebye','hello'] >>> characters.remove('hello') >>> characters ['byebye', 'hello'] #reverse 反序排列列表元素 就地修改不返回值 >>> numbers=[1,2,3,4] >>> numbers.reverse() >>> numbers [4, 3, 2, 1] #sort 就地排序,不返回值 稳定排序算法 >>> x=[1,8,5,3,7,9,4] >>> x.sort() >>> x [1, 3, 4, 5, 7, 8, 9] #sorted >>> x=[1,8,5,3,7,9,4] >>> y=sorted(x) >>> y [1, 3, 4, 5, 7, 8, 9] >>> x [1, 8, 5, 3, 7, 9, 4] #高级sort sort可以接受两个参数key reverse #一般情况下把key设置为一个自定义函数用于排序,类似cmp这样的函数 >>> x=['hahah','jkjkjkjkjkj','lalal'] >>> x.sort(key=len) >>> x ['hahah', 'lalal', 'jkjkjkjkjkj'] #对于reverse只需要将其指定为一个真值(True或False),以指出是否要按相反顺序对列表进行排序,True要逆序 从大到小 False不要逆序从小到大 >>> numbers=[4,8,6,2,3,7,9] >>> numbers.sort(reverse=True) >>> numbers [9, 8, 7, 6, 4, 3, 2] >>> numbers.sort(reverse=False) >>> numbers [2, 3, 4, 6, 7, 8, 9]
三、元组
元组,只需要将一些值按照逗号分开就可以创建一个元组。同时元组与列表很像,只是不能修改
>>> 1,2,3 (1, 2, 3) >>> 1, (1,) >>> 1 1 >>> 3*(40+2) 126 >>> 3*(40+2,) (42, 42, 42) >>> () () >>> tuple([1,2,3]) (1, 2, 3) >>> tuple("jiameng") ('j', 'i', 'a', 'm', 'e', 'n', 'g') >>> tuple((1,2,3)) (1, 2, 3)
元组可以用作映射中的键,而列表不可以,有些内置函数或者方法返回元组。