List的常用函数
简单的函数有:
- cmp(list1,list2) 比较两个列表的元素 (python3.0后删去,直接用>,==,<即可)
- len(list) 列表元素个数
- max(list) 返回列表元素最大值
- min(list) 返回列表元素最小值
- list(seq) 将可迭代容器转换为列表 (如果是字典的话,就只保留键)
接下的函数最好单独一行,不要出现print(list.append())这样的语句,会打印出None,而不是所期望的值。
-
list1.append(obj)
- 末尾增加元素
pythonaList = [123, 'xyz', 'zara', 'abc'] aList.append( 2009 ) print ("Updated List : ", aList) # result Updated List : [123, 'xyz', 'zara', 'abc', 2009]
-
list2.count(obj)
- 统计obj的个数
pythonaList = [123, 'xyz', 'zara', 'abc', 123] print ("Count for 123 : ", aList.count(123)) print ("Count for zara : ", aList.count('zara')) # result Count for 123 : 2 Count for zara : 1
-
list3.count(obj)
- extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
pythonlist1 = [1,2,3,4] dict1 = {'a':1,'b':2} list1.extend(dict1) print(list1) # result [1, 2, 3, 4, 'a', 'b'] aList = [123, 'xyz', 'zara', 'abc', 123] bList = [2009, 'manni'] aList.extend(bList) print ("Extended List : ", aList) # result Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
-
list4.index(x[, start[, end]])
- x-- 查找的对象。
- start-- 可选,查找的起始位置。
- end-- 可选,查找的结束位置。
pythonaList = ['runoob', 'xyz', 'runoob', 'abc'] print ("runoob 索引位置: ", aList.index( 'runoob' )) print ("runoob 索引位置 : ", aList.index( 'runoob', 1, 3 )) # result runoob 索引位置: 0 runoob 索引位置 : 2
-
list5.insert()
- insert(x,obj) 函数用于将指定对象插入列表的指定位置。(这里的插入是指第x对象的前面)
pythonaList = [123, 'xyz', 'zara', 'abc'] blist = [1,2,3,5,6] aList.insert(3, blist) print("Final List : ", aList) # result Final List : [123, 'xyz', 'zara', [1, 2, 3, 5, 6], 'abc']
-
list6.pop()
- pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
- list.pop([index=-1])
- obj -- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
pythonlist1 = ['Google', 'Runoob', 'Taobao'] list_pop = list1.pop(1) print("删除的项为 :", list_pop) print("列表现在为 : ", list1) # result 删除的项为 : Runoob 列表现在为 : ['Google', 'Taobao']
-
list7.remove()
- remove() 函数用于移除列表中某个值的第一个匹配项。
- list.remove(obj)
- obj -- 列表中要移除的对象
pythonaList = [123, 'xyz', 'zara', 'abc', 'xyz'] aList.remove('xyz') print ("List : ", aList) aList.remove('abc') print ("List : ", aList) # result List : [123, 'zara', 'abc', 'xyz'] List : [123, 'zara', 'xyz']
-
list8.reverse()
- reverse() 函数用于反向列表中元素。
- list.reverse()
pythonaList = [123, 'xyz', 'zara', 'abc', 'xyz'] aList.reverse() print ("List : ", aList) # result List : ['xyz', 'abc', 'zara', 'xyz', 123]
-
list9.sort()
- sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
- list.sort(cmp=None, key=None, reverse=False)
- cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
- key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
- reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
- 值得注意的是排序语句只能单独一条 不能和其他的语句如print一起使用,如
print(list1.sort())
不会报错但是打印出来是None。
pythondef takeSecond(elem): return elem[1] # 列表 random = [(2, 2), (3, 4), (4, 1), (1, 3)] # 指定第二个元素排序 random.sort(key=takeSecond) # 输出类别 print('排序列表:') print(random) # result 排序列表: [(4, 1), (2, 2), (1, 3), (3, 4)]
-
list10.extendI(seq)
- list.extend(seq)
- seq -- 元素列表。
pythonaList = [123, 'xyz', 'zara', 'abc', 123] bList = [2009, 'manni'] aList.extend(bList) print ("Extended List : ", aList ) # result Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']