python中的middle_python中各种结构的复杂度

list

The Average Case assumes parameters generated uniformly at random.

Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.

OperationAverage Case

CopyO(n)O(n)

Append[1]O(1)O(1)

InsertO(n)O(n)

Get ItemO(1)O(1)

Set ItemO(1)O(1)

Delete ItemO(n)O(n)

IterationO(n)O(n)

Get SliceO(k)O(k)

Del SliceO(n)O(n)

Set SliceO(k+n)O(k+n)

Extend[1]O(k)O(k)

O(n log n)O(n log n)

MultiplyO(nk)O(nk)

x in sO(n)

min(s), max(s)O(n)

Get LengthO(1)O(1)

collections.deque

A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.

OperationAverage CaseAmortized Worst Case

CopyO(n)O(n)

appendO(1)O(1)

appendleftO(1)O(1)

popO(1)O(1)

popleftO(1)O(1)

extendO(k)O(k)

extendleftO(k)O(k)

rotateO(k)O(k)

removeO(n)O(n)

set

See dict -- the implementation is intentionally very similar.

OperationAverage caseWorst Case

x in sO(1)O(n)

Intersection s&tO(min(len(s), len(t))O(len(s) * len(t))

Difference s-tO(len(s))

s.difference_update(t)O(len(t))

Symmetric Difference s^tO(len(s))O(len(s) * len(t))

s.symmetric_difference_update(t)O(len(t))O(len(t) * len(s))As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different! The first one is O(len(s)) (for every element in s add it to the new set, if not in t). The second one is O(len(t)) (for every element in t remove it from s). So care must be taken as to which is preferred, depending on which one is the longest set and whether a new set is needed.

To perform set operations like s-t, both s and t need to be sets. However you can do the method equivalents even if t is any iterable, for example s.difference(l), where l is a list.

dict

The Average Case times listed for dict objects assume that the hash function for the objects is sufficiently robust to make collisions uncommon. The Average Case assumes the keys used in parameters are selected uniformly at random from the set of all keys.

Note that there is a fast-path for dicts that (in practice) only deal with str keys; this doesn't affect the algorithmic complexity, but it can significantly affect the constant factors: how quickly a typical program finishes.

OperationAverage CaseAmortized Worst Case

Copy[2]O(n)O(n)

Get ItemO(1)O(n)

Set Item[1]O(1)O(n)

Delete ItemO(1)O(n)

Iteration[2]O(n)O(n)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值