Python列表(list)的操作及方法

2023-10-31 08:48:21

list操作

list内置方法

  • append(self, object)

用途:在list的末尾添加一个object。

返回值:返回一个添加object的list。

       其中,object表示被添加目标(可以是任意类型)。

例:

>>> test = ['python']
>>> test.append(3)
>>> test
['python', 3]  #输出结果

>>> test.append([1, 2])  #添加一个列表
>>> test
['python', 3, [1, 2]]  #输出结果

>>> test.append((5, 4,))  #添加一个元组
>>> test
['python', 3, [1, 2], (4, 5)]  #输出结果

>>> test.append(2.7)  #添加一个小数
>>> test
['python', 3, [1, 2], (4, 5), 2.7]
  • clear(self)

用途:将list清空。

返回值:返回一个空的list。

例:

>>> test = ["python"]
>>> test
[]  #输出结果,一个空的列表
  • copy(self)

用途:复制一份列表。

返回值:返回列表的一个副本(修改副本不会改变原有的列表)。

例:

>>> test = ["python"]
>>> testCopy = test.copy()
>>> testCopy
["python"]  #输出结果

>>> testCopy.append(3)
>>> testCopy
["python", 3]  #输出结果

>>> test
["python"]  #输出结果,改变其副本,并不会改变原有列表
  • count(self, object)

用途:在list中统计object的个数。

返回值:返回object的个数。

       其中,object表示被统计的元素(可以是任意类型)。

例:

>>> test = ["python"]
>>> test.count("python")
1  #输出结果

>>> test.append("python")
>>> test.count("python")
2  #输出结果
  • extend(self, iterable)

用途:在list的末尾添加元素。(注:区分append和extend的区别)

返回值:返回在列表中添加元素后的list。

       其中,iterable表示能够迭代的元素(数字不能迭代)。

例:

>>> test = ["python"]
>>> test.extend([1, 2, 3])
>>> test
['python', 1, 2, 3]  #输出结果

>>> test = ["python"]
>>> test.append([1, 2, 3])  #区分extend和append
>>> test
['python', [1, 2, 3]]  #输出结果

>>> test = ["python"]
>>> test.extend("extend")
>>> test
['python', 'e', 'x', 't', 'e', 'n', 'd']  #输出结果,如果是字符串,则会一个字符一个字符的迭代添加

>>> test.extend((1,2,3,))  #添加一个元组
>>> test
['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]  #输出结果

>>> test.extend(666)  #数字不能迭代
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    test.extend(666)
TypeError: 'int' object is not iterable
  • index(self, object, start, stop)

用途:在list中查找object的索引。

返回值:返回object在list中的的位置索引。

       其中,object表示需要查找的元素;

                 start表示list中查找的起始位置;

                 stop表示list中查找的末尾位置。

例:

>>> test = ['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]
>>> test.index("e")
1  #输出结果,从左边至又开始查,如果有相同的元素,只会返回第一个元素的索引

>>> test.index("e", 2, 9)  #指定起始位置和终止位置
4
  • insert(self, index, object)

用途:在list中的index位置插入object元素。

返回值:返回插入元素object后的list。

       其中,index表示插入元素的位置;

                 object表示插入的元素。

例:

>>> test = ["python", "insert"]
>>> test.insert(1, [2, 3])  #在索引1处插入列表[2, 3]
>>> test
['python', [2, 3], 'insert']  #输出结果

>>> test.insert(2, (666, 888))  #在索引2处插入元组(666, 888)
>>> test
['python', [2, 3], (666, 888), 'insert']  #输出结果
  • pop(self, index)

用途:在list中的移除index位置元素。

返回值:返回移除指定index处的元素。

       其中,index表示移除元素的位置(默认为list末尾的元素索引)。

例:

>>> test = ['python', [2, 3], (666, 888), 'insert']
>>> test.pop()
'insert'  #输出结果

>>> test.pop(1)
[2, 3]  #输出结果

>>> test
['python', (666, 888)]  #输出结果
  • remove(self, object)

用途:在list中的移除object元素。

返回值:无。

       其中,object表示要被移除的元素。

       例:

>>> test = ['python', (666, 888)]
>>> test.remove('python')
>>> test
[(666, 888)]  #输出结果
  • reverse(self)

用途:将list中所有的元素的进行反转。

返回值:无。

       例:

>>> test = ['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]
>>> test.reverse()
>>> test
[3, 2, 1, 'd', 'n', 'e', 't', 'x', 'e', 'python']  #输出结果
  • sort(self, key, reverse)

用途:将list中所有的元素进行排序。

返回值:无。

       其中,reverse如果为True则以逆顺进行排序,默认为False,则为正顺排序

       例:

>>> test = [3, 1, 999, 3, 5, 234]
>>> test.sort()
>>> test
[1, 3, 3, 5, 234, 999]  #输出结果

>>> test.sort(reverse = True)  #逆顺排序
>>> test
[999, 234, 5, 3, 3, 1]  #输出结果

>>> test = [3, 2, 1, 'd', 'n', 'e', 't', 'x', 'e', 'python']
>>> test.sort()  #字符串和数字不能混合排序
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    test.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

>>> test = ["python", 'insert']
>>> test.sort()  #对字符串进行排序
>>> test
['insert', 'python']  #输出结果
  • 作者:5G加油站
  • 原文链接:https://blog.csdn.net/qq_33206497/article/details/82560146
    更新时间:2023-10-31 08:48:21