python 基础(二),python基础知识


List:

append() :

1 # append(obj) 在列表末尾添加obj这个新的对象
2 L = [1,3,5,7,9]
3 obj = 888
4 L.append(obj)
5 print(L)

insert():

1 #insert(index,obj) 将obj对象插入列表的index位置
2 L = [1,3,5,7,9]
3 obj = 'planBinary'
4 L.insert(0,obj)
5 print(L)

remove()/del()/pop():

1 #remove(obj) 删除列表里的obj对象,如果多个则删除第一个
2 L = ['planBinary',1,3,5,7,9]
3 L.remove('planBinary')
4 print(L)
1 del(reference) 删除一个对象的一个引用,而不是删除一个对象
2 L = ['planBinary',1,3,5,7,9]
3 x = L
4 del(L)
5 print(x)
6 print(L)
1 #pop(index) 移除list下标为index的元素,并且返回该值
2 L = ['planBinary', 1, 3, 5, 7, 9]
3 print(L.pop(0))

index():

 

1 #index(obj,[start,[end]]) 返回obj在列表中第一次出现的位置
2 L = ['planBinary', 9, 3, 1, 1, 9]
3 print(L.index(1))
4 print(L.index('planBinary'))

 

count():

 

1 #count(obj,[start,[end]]) 统计obj在列表里出现的次数
2 L = ['planBinary', 9, 3, 1, 1, 9]
3 print(L.count(1))

 

clear():

 

1 #claer() 清空列表
2 L = ['planBinary', 9, 3, 1, 1, 9]
3 L.clear()
4 print(L)

 

sort():

 

1 #sort() 对列表里的元素按ascii码值排序,只能字符串和字符串,数字和数字排序,不能混合排序
2 L = ['planBinary', 'sda', 'adasd', 'dasd']
3 L.sort()
4 print(L)

 

reverse():

 

1 #reversre() 将列表倒叙排序
2 L = ['planBinary', 'sda', 'adasd', 'dasd']
3 L.reverse()
4 print(L)

 

extend():

 

1 #extend(obj) 在列表末尾追加obj对象的所以值,这个对象可以是列表,元组,集合,字典。字典追加key的值
2 L_1= ['planBinary', 'sda', 'adasd', 'dasd']
3 L_2 = [ 9, 3, 1, 1, 9]
4 L_1.extend(L_2)
5 print(L_1)

 

copy():

1 #copy() 复制一个列表,如果是多层列表,只复制第一层列表,第二层列表以及更高层复制的是地址
2 L_1 = [['planBinary','age','handsome'], 'sda', 'adasd', 'dasd']
3 L_2 = L_1.copy()
4 print(L_2)
5 L_2[0][2]  = 'very_handsome'
6 print(L_1)

 

 

 

 

 

 

 

 

 

相关内容

    暂无相关文章

评论关闭