Python基础-list的各种操作
2017-04-11 00:07
483 查看
以下是list数据类型的各种操作
Add an item to the end of the list; equivalent to
Extend the list by appending all the items in the given list; equivalent to
Insert an item at a given position. The first argument is the index of the element before which to insert, so
Remove the first item from the list whose value is x. It is an error if there is no such item.
Remove the item at the given position in the list, and return it. If no index is specified,
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Return the number of times x appears in the list.
Sort the items of the list in place (the arguments can be used for sort customization, see
Reverse the elements of the list, in place.
You might have noticed that methods like
你可能注意到了,像insert,remove和sort方法只是改变了list的数值而没有输出,因为他们的返回值送None,这是Python中所有可变数据结构的设计原则。
例如
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(i, x) 在指定位置插入一个数据
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=None, key=None, reverse=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
insert,
removeor
sortthat 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-模块和包.深入SQLAlchemy之玩转各种增删查改操作?
- Python基础:list列表以及序列常用操作
- python基础教程共60课-第26课操作list
- Python中list的各种操作
- python基础--list操作
- python之LIST基础操作
- [零基础学python]用Python操作数据库(2)
- Python中的基本list操作
- Python 使用 Xlrd/xlwt 操作 Excel 分类: python基础学习 2014-03-17 12:06 958人阅读 评论(0) 收藏
- (转载)Python 列表(list)操作
- Selenium基于Python web自动化基础一 -- 基础汇总及简单操作
- Python3基础(六) 深入list列表
- Python列表(list)、字典(dict)、字符串(string)基本操作小结
- 给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3
- python基础之列表常用操作及知识点小结
- 【Python】基础学习——列表(list),元祖(tuple),字典(dict),字符串(string),集合(set)
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python list操作 - 转