Python:数据结构

List:

list.append(x)

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

list.extend(L)

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

list.insert(i,x)

Insert an item at a given position. The first argument is the index of theelement before which to insert, soa.insert(0,x) inserts at the front ofthe list, anda.insert(len(a),x) is equivalent toa.append(x).

list.remove(x)

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

list.pop([i])

Remove the item at the given position in the list, and return it. If no indexis specified,a.pop() removes and returns the last item in the list. (Thesquare brackets around thei in the method signature denote that the parameteris optional, not that you should type square brackets at that position. Youwill see this notation frequently in the Python Library Reference.)

list.index(x)

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

list.count(x)

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 sortcustomization, see sorted() for their explanation).

list.reverse()

Reverse the elements of the list, in place

例子:
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> a.pop()
1234.5
>>> a
[-1, 1, 66.25, 333, 333]



把列表作为栈:

列表的方法使其非常容易作为一个栈使用:最后添加的元素最先被恢复(后入先出),使用append()函数再栈顶压入数据,使用pop()函数从栈顶弹出数据,其无需索引号。

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]


把列表作为队列:

也可以把列表作为队列使用:最先添加的元素最先被灰度(先入先出)。但是其实现并不是很高效,虽然从列表的末尾添加和移除数据很快,但是从列表的开头却很慢(因为其他元素需要逐个后移)。

想要实现一个队列,可以使用collections.deque ,其可以从两端快速添加和移除数据。

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

函数式编程工具:

有三个内建函数在处理列表时非常有用:filter(),map(),reduce()

>>> def f(x): return x % 3 == 0 or x % 5 == 0
...
>>> filter(f, range(2, 25))
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

参考文献:https://docs.python.org/2/tutorial/datastructures.html#data-structures

                    https://docs.python.org/2/tutorial/introduction.html#lists






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值