Python基础-list的各种操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础-list的各种操作相关的知识,希望对你有一定的参考价值。

以下是list数据类型的各种操作

list.append(x)    给list末尾添加一个元素

Add an item to the end of the list; equivalent to a[len(a):] [x].

list.extend(L)    添加一组数据到list 的末尾

Extend the list by appending all the items in the given list; equivalent to a[len(a):] L.

list.insert(ix) 在指定位置插入一个数据

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

list.remove(x)   删除list的第一个x数据

Remove the first item from the list whose value is x. It is an error if there is no such item.

list.pop([i])     删除list中指定索引位置的元素,如果不加索引【list.pop()】,则删除的是最后一个元素

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

list.index(x)  得到list中的元素对应的索引

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x)  统计list中的元素的个数

Return the number of times x appears in the list.

list.sort(cmp=Nonekey=Nonereverse=False)

Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

list.reverse()

Reverse the elements of the list, in place.

You might have noticed that methods like insertremove or sort that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.

你可能注意到了,像insert,remove和sort方法只是改变了list的数值而没有输出,因为他们的返回值送None,这是Python中所有可变数据结构的设计原则。

例如

a = [1, 2, 333, -1, 333, 13.89, ‘Test‘]
a.append(4)
# [1, 2, 333, -1, 333, 13.89, ‘Test‘, 4]
a.extend(range(1, 11))
# [1, 2, 333, -1, 333, 13.89, ‘Test‘, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.count(333)
# 2
a.insert(1, ‘a‘)
# [1, ‘a‘, 2, 333, -1, 333, 13.89, ‘Test‘, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.remove(1)
# [‘a‘, 2, 333, -1, 333, 13.89, ‘Test‘, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.pop()
# [‘a‘, 2, 333, -1, 333, 13.89, ‘Test‘, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.pop(3)
# [‘a‘, 2, 333, 333, 13.89, ‘Test‘, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print a.index(2)
# 1
print a.count(333)
# 2
a.sort()
# [1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 13.89, 333, 333, ‘Test‘, ‘a‘]
a.reverse()
# [‘a‘, ‘Test‘, 333, 333, 13.89, 9, 8, 7, 6, 5, 4, 4, 3, 2, 2, 1]

Using Lists as Stacks(使用list作为栈)

利用list的方法可以简单的实现栈,由于栈是“后进先出”原则,可以利用append()和pop()方法来模拟栈

stack = [1, 2, 3]
stack.append(4)
stack.append(5)
# [1, 2, 3, 4, 5]
stack.pop()
# [1, 2, 3, 4]

 

Using Lists as Queues(使用list作为队列)

利用list的方法可以实现队列,由于队列是“先进先出原则”,可以利用append()和pop()方法,但是这不是最有效的,因为这样,每个元素的都有对应的移动一位。要实现一个队列,请使用collections.deque,它被设计为从两端快速添加和弹出。

from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")
queue.append("Graham")
# deque([‘Eric‘, ‘John‘, ‘Michael‘, ‘Terry‘, ‘Graham‘])
queue.popleft()
queue.popleft()
# deque([‘Michael‘, ‘Terry‘, ‘Graham‘])

 

以上是关于Python基础-list的各种操作的主要内容,如果未能解决你的问题,请参考以下文章

Python基础--“苦力”列表

python基础

Python列表各种操作

Python基础之各种推导式玩法

python基础--1.数据操作

python基础_集合文件操作