python中列表的方法使用

一. 列表的方法使用

  1. Python List append()方法

  描述:append() 方法用于在列表末尾添加新的对象。

  语法:list.append(obj)

  参数:obj -- 添加到列表末尾的对象。

  返回值:该方法无返回值,但是会修改原来的列表。

  实例:

  list1 = [1,25,698,585,20,0]

  list1.append(200)

  print(list1)

  以上实例输出结果如下:

  [ 1, 25, 698, 585, 20, 0,200]

  2. Python List insert()方法

  描述:insert() 函数用于将指定对象插入列表的指定位置。

  语法:list.insert(index, obj)

  参数:index -- 对象 obj 需要插入的索引位置。obj -- 要插入列表中的对象。

  返回值:该方法没有返回值,但会在列表指定位置插入对象。

  实例:

  list1 = [1,25,698,585,20,0]

  list1.insert(0,2019)

  print(list1)

  以上实例输出结果如下:

  [2019, 1, 25, 698, 585, 20, 0]

  3. Python List copy()方法

  实例:

  list1 = [1,25,698,585,20,0]

  list2=list1.copy()

  print(list2)

  以上实例输出结果如下:

  [1, 25, 698, 585, 20, 0]

  4. Python List count()方法

  描述:count() 方法用于统计某个元素在列表中出现的次数。

  语法:list.count(obj)

  返回值:返回元素在列表中出现的次数。

  实例:

  list1 = [1,25,698,585,20,0,25]

  print(list1.count(25))

  以上实例输出结果如下:

  2

  5. Python List index()方法

  描述:index() 函数用于从列表中找出某个值第一个匹配项的索引位置。

  语法:list.index(x[, start[, end]])

  参数:x-- 查找的对象。start-- 可选,查找的起始位置。end-- 可选,查找的结束位置。

  返回值:该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。

  实例:

  aList = [123, 'xyz', 'runoob', 'abc','xyz','runoob','ceshi']

  print ("xyz 索引位置: ", aList.index( 'xyz' ))

 print ("runoob 索引位置 : ", aList.index( 'runoob', 3, 6 ))

  以上实例输出结果如下:

  xyz 索引位置: 1

  runoob 索引位置 :  5

  6. Python List remove()方法

  描述:remove() 函数用于移除列表中某个值的第一个匹配项。

  语法:list.remove(obj)

  返回值:该方法没有返回值但是会移除列表中的某个值的第一个匹配项。

  实例:

  aList = [123, 'xyz', 'zara', 'abc', 'xyz'];

  aList.remove('xyz');

  print ("alist:",aList)

  aList.remove('abc');

  print ("alist:",aList)

  以上实例输出结果如下:

  alist: [123, 'zara', 'abc', 'xyz']

  alist: [123, 'zara', 'xyz']

  7. Python List pop()方法

  描述:pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

  语法:list.pop([index=-1])

  参数:obj -- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。

  返回值:该方法返回从列表中移除的元素对象。

  实例:

  list1 = ['Google', 'Runoob', 'Taobao']

  list_pop=list1.pop(2)

  print ("删除的项为 :", list_pop)

  print ("列表现在为 : ", list1)

  以上实例输出结果如下:

  删除的项为 : Taobao

  列表现在为 :  ['Google', 'Runoob']

  8. Python List extend()方法

  描述:extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

  语法:list.extend(seq)

  实例:

  aList = [123, 'xyz', 'zara', 'abc', 123];

  bList = [2009, 'manni'];

  aList.extend(bList)

  print ("Extended List : ", aList)

  以上实例输出结果如下:

  Extended List :  [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']

  9. Python List reverse()方法

  描述:reverse() 函数用于反向列表中元素。

  语法:list.reverse()

  实例:

  aList = [123, 'xyz', 'zara', 'abc', 'xyz','ceshi']

  aList.reverse()

  print ("List : ", aList)

  以上实例输出结果如下:

  List :  ['ceshi', 'xyz', 'abc', 'zara', 'xyz', 123]

  10. Python List sort()方法

  描述:sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

  语法:list.sort(cmp=None, key=None, reverse=False)

  实例1(升序):

  aList = [15, 55, 23, 89, 66];

  aList.sort();

  print("List : ", aList)

  以上实例输出结果如下:

  List :  [15, 23, 55, 66, 89]

  实例1(降序):

  aList = [15, 55, 23, 89, 66];

  aList.sort(reverse=True);

  print("List : ", aList)

  以上实例输出结果如下:

  List :  [89, 66, 55, 23, 15]

  实例1(通过指定列表中的元素排序来输出列表):

  # 获取列表的第二个元素

  def takeSecond(elem):

      return elem[0]

  # 列表

  random = [(2, 2), (3, 4), (4, 1), (1, 3)]

  # 指定第二个元素排序

  random.sort(key=takeSecond)

  # 输出类别

  print('排序列表:', random)

  以上实例输出结果如下:

  排序列表: [(1, 3), (2, 2), (3, 4), (4, 1)]

实例1(升序):

  aList = [15, 55, 23, 89, 66];

  aList.sort();

  print("List : ", aList)

  以上实例输出结果如下:

  List :  [15, 23, 55, 66, 89]

原文地址:https://www.cnblogs.com/zhouxuyang1/p/11836279.html