假期结束了,还没过够就要回来上班了,懈怠了一周,知识还是要补回来的!废话不多说,直接开始!列表知识!
1、创建列表,上例子!需要注意括号类型“[ ]”,除数字外的字符要用 ' ' 引起来哈~
>>> a=[3,'kk',4.3] >>> a [3, 'kk', 4.3]
2、添加列表元素!
>>> a.append('Michelle') >>> a [3, 'kk', 4.3, 'Michelle']
注意这里只能加单个,不能一次添加多个,不信你看!但我仍不死心,打算用列表的形式插入两个数字,结果还是以列表形式插入了,好吧,我还是想别的办法吧!
>>> list1=[8, 7, 6, 4, 2, 1] >>> list1 [8, 7, 6, 4, 2, 1] >>> list1.append(33,22) Traceback (most recent call last): File "<pyshell#79>", line 1, in <module> list1.append(33,22) TypeError: append() takes exactly one argument (2 given) >>> list1.append([33,22]) >>> list1 [8, 7, 6, 4, 2, 1, [33, 22]]
3、办法就是扩展列表命令!extend!走你!一大片是不?我给你解释解释,这片代码的逻辑就是定义列表-添加一个元素-在列表力扩展多个元素,extend就是添加多个元素的解决方法,不过有注意到了没,当我直接将多个字符直接括在extend里面,运行时显示是错误的,这是因为没有加列表括号哇==>"[ ]"
>>> a=[3,'kk',4.3]
>>> a
[3, 'kk', 4.3]
>>> a.append('Michelle')
>>> a
[3, 'kk', 4.3, 'Michelle']
>>> a.extend('hello','hi')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
a.extend('hello','hi')
TypeError: extend() takes exactly one argument (2 given)
>>> a.extend(['hello','hi'])
>>> a
[3, 'kk', 4.3, 'Michelle', 'hello', 'hi']
4、插入命令,insert!这里解释一下,insert括号里的第一位数字是列表里字符的顺序序数,这里在补充一下,python数字序数都是以0开头,也就是说如果定义一个别表里的第一个数,你要从零开始数,一次类推;序数后面的就是你要插入的字符了,以下命令说白了就是在列表第一位插入cute,还是很好理解的!
>>> a.insert(0,'cute') >>> a ['cute', 3, 'kk', 4.3, 'Michelle', 'hello', 'hi']
5、列表元素交换位置,这个逻辑就是,当你想让两个元素点换位置时,先让一个元素挪到那个位置再将另一转换成另一个字符,文字描述好绕,看代码吧!
>>> c=a[3] >>> a[3]=a[4] >>> a ['cute', 'kk', 3, 'Michelle', 'Michelle', 'hello', 'hi'] >>> a[4]=c >>> a ['cute', 'kk', 3, 'Michelle', 4.3, 'hello', 'hi']
6、撤销命令!用remove后面括起来的是这个元素本身
>>> a=['cute', 'kk', 3, 'Michelle', 4.3, 'hello', 'hi'] >>> a.remove(3) >>> a ['cute', 'kk', 'Michelle', 4.3, 'hello', 'hi']
或者用del,这个后面括起来的是元素所在序数,这不,我把第二个‘kk’给删了,哈哈~
>>> del a[1] >>> a ['cute', 'Michelle', 4.3, 'hello', 'hi']
pop也有剔除某个元素的效果
>>> a ['cute', 'Michelle', 4.3, 'hello', 'hi'] >>> a.pop() 'hi' >>> a.pop(1) 'Michelle' >>> a ['cute', 4.3, 'hello'] >>> a[0:1] ['cute']
7、列表合并,叠加以及识别某元素是否在内,这个很简单,就不啰嗦啦
>>> list1=[2,4,6,7,8,1] >>> list2=[43,35,67,12] >>> list3=list1+list2 >>> list3 [2, 4, 6, 7, 8, 1, 43, 35, 67, 12] >>> list3*2 [2, 4, 6, 7, 8, 1, 43, 35, 67, 12, 2, 4, 6, 7, 8, 1, 43, 35, 67, 12] >>> 0 in list3 False >>> 3 in list3 False >>> 12 in list3 True >>> 0 not in list3 True
8、count就是帮你数一下括号里的字符在列表里出现几次
>>> list3.count(1) 1 >>> list5=list3*2 >>> list5 [2, 4, 6, 7, 8, 1, 43, 35, 67, 12, 2, 4, 6, 7, 8, 1, 43, 35, 67, 12] >>> list5.count(1) 2
index是告诉你括号里的字符在列表里第几位,第一个好理解,第二的代码中3,7是范围,也就是在列表第四位到第八位之间找到1所在的位数,这里还要再次提醒,P的数数方式是从零开始的哈~
>>> list5.index(4) 1 >>> list5.index(1,3,7) 5
reverse,顾名思义,就是反转、倒过来的意思啦
>>> list5.reverse() >>> list5 [12, 67, 35, 43, 1, 8, 7, 6, 4, 2, 12, 67, 35, 43, 1, 8, 7, 6, 4, 2]
sort,有小到大排列,从大到小的话就用reverse就好了嘛,True首字母一定要大写哇
>>> list5.sort () >>> list5 [1, 1, 2, 2, 4, 4, 6, 6, 7, 7, 8, 8, 12, 12, 35, 35, 43, 43, 67, 67] >>> list5.sort(reverse=True) >>> list5 [67, 67, 43, 43, 35, 35, 12, 12, 8, 8, 7, 7, 6, 6, 4, 4, 2, 2, 1, 1]
拷贝列表,用:,若仅拷贝范围就如list7那样给范围就好~
>>> list6=list5[:] >>> list6 [67, 67, 43, 43, 35, 35, 12, 12, 8, 8, 7, 7, 6, 6, 4, 4, 2, 2, 1, 1] >>> list7=list5[3:9] >>> list7 [43, 35, 35, 12, 12, 8]
小知识点比较多,我得好好消化一下,加油!继续在学习Python的道路上吭哧吭哧的走下去吧!