len是O(n)还是O(1)?

Python使用数组列表对吗?
DEF QUICKSORT(LISTA):
如果Lista == []:
Lista
别的:
返回QuickSort([x for x in lista [1:]如果x QuickSort([x for x in lista [1:]如果x> = lista [0]])
或者
DEF QUICKSORT(LISTA):
如果Len(Lista)== 0
Lista
别的:
返回QuickSort([x for x in lista [1:]如果x QuickSort([x for x in lista [1:]如果x> = lista [0]])
等待第一个提高Undpulported操作数类型的Typeerror.

# 回答1


在2008年9月11日的星期四02:23:43 -0700,过程写道:
`len()`on List`对象是o(1).
两者都这样做是因为``none' +'列表没有意义.你应该实际上应该
````返回''````如果
对该名称的对象,然后对此无能为力.
较短的选择:
如果不是Lista:
返回 []
空列表在布尔测试中被视为" false".
再见,
marc'blackjack'rintsch
# 回答2


过程写道:
Len是O(1).有关其他操作,请参见http://effbot.org/zone/python-list.htm#performance
最好做到这一点:
DEF QUICKSORT(LISTA):
如果不是Lista:
返回列表
...
# 回答3


好的,但是如果Len是O(1),那没关系吗?相比
如果不是Lista:
返回 []
我的意思是.
# 回答4


过程写道:
没关系?
# 回答5


取决于测试的频率,例如:
...如果不是:返回[]
...
2 0 load_fast 0(a)
3 jump_if_true 5(到11)
6 POP_TOP
7 build_list 0
10 return_value
12 load_const 0(无)
15 return_value
...如果不是Len(A):返回[]
...
2 0 LOAD_GLOBAL 0(LEN)
3 load_fast 0(a)
6 call_function 1
9 jump_if_true 5(到17)
12 pop_top
13 build_list 0
16 return_value
18 load_const 0(无)
21 return_value
即使Len(a)是O(1),它将具有更大的常数
系数是因为查找并致电Len().
跳过

标签: python

添加新评论