简体   繁体   English

python 列表函数的运行时复杂度是多少?

[英]What is the runtime complexity of python list functions?

I was writing a python function that looked something like this我正在写一个 python function 看起来像这样

def foo(some_list):
   for i in range(0, len(some_list)):
       bar(some_list[i], i)

so that it was called with所以它被称为

x = [0, 1, 2, 3, ... ]
foo(x)

I had assumed that index access of lists was O(1) , but was surprised to find that for large lists this was significantly slower than I expected.我曾假设列表的索引访问是O(1) ,但惊讶地发现对于大型列表,这比我预期的要慢得多。

My question, then, is how are python lists are implemented, and what is the runtime complexity of the following那么,我的问题是 python 列表是如何实现的,以下的运行时复杂度是多少

  • Indexing: list[x]索引: list[x]
  • Popping from the end: list.pop()从末尾弹出: list.pop()
  • Popping from the beginning: list.pop(0)从头弹出: list.pop(0)
  • Extending the list: list.append(x)扩展列表: list.append(x)

For extra credit, splicing or arbitrary pops.对于额外的信用、拼接或任意流行音乐。

there is a very detailed table on python wiki which answers your question. python wiki 上有一个非常详细的表格可以回答您的问题。

However, in your particular example you should use enumerate to get an index of an iterable within a loop.但是,在您的特定示例中,您应该使用enumerate来获取循环内可迭代对象的索引。 like so:像这样:

for i, item in enumerate(some_seq):
    bar(item, i)

The answer is "undefined".答案是“未定义”。 The Python language doesn't define the underlying implementation. Python 语言没有定义底层实现。 Here are some links to a mailing list thread you might be interested in.以下是您可能感兴趣的邮件列表线程的一些链接。

Also, the more Pythonic way of writing your loop would be this:此外,编写循环的更 Pythonic 方式是这样的:

def foo(some_list):
   for item in some_list:
       bar(item)

Lists are indeed O(1) to index - they are implemented as a vector with proportional overallocation, so perform much as you'd expect.列表确实是 O(1) 到索引——它们被实现为一个具有比例过度分配的向量,所以表现得像你期望的那样。 The likely reason you were finding this code slower than you expected is the call to " range(0, len(some_list)) ".您发现此代码比预期慢的可能原因是对“ range(0, len(some_list)) ”的调用。

range() creates a new list of the specified size, so if some_list has 1,000,000 items, you will create a new million item list up front. range()创建一个指定大小的新列表,因此如果 some_list 有 1,000,000 个项目,您将预先创建一个新的百万项目列表。 This behaviour changes in python3 (range is an iterator), to which the python2 equivalent is xrange , or even better for your case, enumerate这种行为在 python3 中发生了变化(范围是一个迭代器),python2 的等效项是xrange ,或者对你的情况来说更好,枚举

if you need index and value then use enumerate:如果您需要索引和值,请使用枚举:

for idx, item in enumerate(range(10, 100, 10)):
    print idx, item

Python list actually nothing but arrays. Thus, Python 实际上只列出了 arrays。因此,

indexing takes O(1)索引需要 O(1)

for pop and append again it should be O(1) as per the docs对于 pop 和 append,根据文档,它应该是 O(1)

Check out following link for details:查看以下链接了解详情:

http://dustycodes.wordpress.com/2012/03/31/pythons-data-structures-complexity-analysis/ http://dustycodes.wordpress.com/2012/03/31/pythons-data-structures-complexity-analysis/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM