python初学(二)
2019年3月10号
..............................................................................................
列表:在python中,用方括号表示一个列表——[x],x可以为数字、字符串或其他类型的对象,也可以为空
定义一个空列表(列表为空时,用bool函数判断为False)
>>> a=[]
>>> type(a)
<class 'list'>
>>> bool(a)
False
>>> print(a)
[]
定义一个非空列表(列表不为空时,用bool函数判断为True)
>>> a=['2','gfhsfh']
>>> type(a)
<class 'list'>
>>> bool(a)
True
>>> print(a)
['2', 'gfhsfh']
可在列表下填一个列表,如下
>>> a=['4','akshd']
>>> b=['2',a]
>>> type(b)
<class 'list'>
>>> print(b)
['2', ['4', 'akshd']]
...................................................................................................
索引和切片(索引从左边开始以0编号,可为负数)
字符串中的索引
>>> url="www.baidu.com"
>>> url[4] //从0计数,取url中的第五位
'b'
>>> url[:5] //从第一位开始取5位
'www.b'
>>> url[3:9] //从0开始计数,第四位取到第九位
'.baidu'
列表的索引
>>> a=['5','fsdj','45','fsfgsg']
>>> a[-1]
'fsfgsg'
>>> a[2] //从0计数,取第三位
'45'
>>> a[5] //超过列表的长度,报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
列表的二次切片
>>> a=['5','fsdj','45','fsfgsg']
>>> a[1][2] //取列表中的第二位中的第三个对象
'd'
根据对象可查找其在列表中的位置
>>> a=['5','fsdj','45','fsfgsg']
>>> a.index("45")
2
..............................................................................................
反转操作 reversed()函数
>>> a="fsdh452:"
>>> list(reversed(a)) //将字符串反转后以列表的形式输出
[':', '2', '5', '4', 'h', 'd', 's', 'f']
reverse()函数,将列表的元素反转
>>> a=[1,2,3,4]
>>> a.reverse() //a不能为字符串,否则报错,无返回值
>>> a
[4, 3, 2, 1]
..............................................................................................
修改列表元素
>>> city=["nan","chan"]
>>> city[1]="jing"
>>> city
['nan', 'jing']
添加列表元素,append()函数,将元素置于列表最后
>>> city=["nan","chan"]
>>> city.append("jing")
>>> city
['nan', 'chan', 'jing']
>>> a=["bei","jing"]
>>> city.append(a)
>>> city
['nan', 'chan', 'jing', ['bei', 'jing']]
将两个列表合并,extend()函数,将元素置于列表后
>>> you=[1,3,1,4]
>>> me=["I","Love","You"]
>>> you.extend(me) //此时该语句没有返回值,只是将“me”中的内容合并到“you”中
>>> you
[1, 3, 1, 4, 'I', 'Love', 'You']
>>> me
['I', 'Love', 'You']
列表与字符串合并会将字符串转换位列表再合并,和整型合并则报错
>>> me=["I","Love","You"]
>>> he="go"
>>> me.extend(he)
>>> me
['I', 'Love', 'You', 'g', 'o']
>>> she=520
>>> me.extend(she)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
insert(i:x)函数,向列表中的“i”位置添加元素“x”。当“i”超过最大索引值,“x”会自动插入列表的末尾
>>> a=["fsdf","fsf","fghs"]
>>> a.insert(0,"hello")
>>> a
['hello', 'fsdf', 'fsf', 'fghs']
>>> a.insert(10,"hello")
>>> a
['hello', 'fsdf', 'fsf', 'fghs', 'hello']
删除列表元素
remove(“x”)函数,若x在列表中即删除第一个符合条件的对象,否则报错
>>> a
['hello', 'fsdf', 'fsf', 'fghs', 'hello']
>>> a.remove("hello")
>>> a
['fsdf', 'fsf', 'fghs', 'hello']
>>> a.remove("world")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
pop(x)函数,删除列表中索引为x的元素,为空则删除列表的最后一位,超出索引长度则报错
>>> a=["fsd","hello","gfsd","hello"]
>>> a.pop(1)
'hello'
>>> a
['fsd', 'gfsd', 'hello']
>>> a.pop()
'hello'
>>> a
['fsd', 'gfsd']
>>> a.pop(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
..............................................................................................
count()函数,统计列表中某个元素的个数
>>> a=[1,2,3,4,1,1,1,3]
>>> a.count(1)
4
sort()函数,将列表的元素排序(默认为从小到大)
>>> a=[54,4,21,84554,21]
>>> a.sort()
>>> a
[4, 21, 21, 54, 84554]
从大到小排序
>>> a.sort(reverse=True)
>>> a
[84554, 54, 21, 21, 4]