What is the runtime complexity of python list functions?

北战南征 提交于 2019-11-26 16:31:13

问题


I was writing a python function that looked something like this

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.

My question, then, is how are python lists are implemented, and what is the runtime complexity of the following

  • Indexing: list[x]
  • Popping from the end: list.pop()
  • Popping from the beginning: list.pop(0)
  • Extending the list: list.append(x)

For extra credit, splicing or arbitrary pops.


回答1:


there is a very detailed table on python wiki which answers your question.

However, in your particular example you should use enumerate to get an index of an iterable within a loop. like so:

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



回答2:


The answer is "undefined". The Python language doesn't define the underlying implementation. Here are some links to a mailing list thread you might be interested in.

  • It is true that Python's lists have been implemented as contiguous vectors in the C implementations of Python so far.

  • I'm not saying that the O() behaviours of these things should be kept a secret or anything. But you need to interpret them in the context of how Python works generally.

Also, the more Pythonic way of writing your loop would be this:

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



回答3:


Lists are indeed O(1) to index - they are implemented as a vector with proportional overallocation, so perform much as you'd expect. The likely reason you were finding this code slower than you expected is the call to "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. This behaviour changes in python3 (range is an iterator), to which the python2 equivalent is xrange, or even better for your case, enumerate




回答4:


Can't comment yet, so

if you need index and value then use enumerate:

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



回答5:


Python list actually nothing but arrays. Thus,

indexing takes O(1)

for pop and append again it should be O(1) as per the docs

Check out following link for details:

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



来源:https://stackoverflow.com/questions/1005590/what-is-the-runtime-complexity-of-python-list-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!