>>> from operator import length_hint>>> l = ["apple", "orange", "banana"]>>> len(l)3>>> length_hint(l)3
>>> list_iterator = iter(l)>>> len(list_iterator)TypeError: object of type 'list_iterator' has no len()>>> length_hint(list_iterator)3
class MyList(object):def __init__(self):self._data = []self.length = 0 # length tracker that takes up memory but makes length op O(1) time
# the implicit iterator in a list classdef __iter__(self):for elem in self._data:yield elem
def add(self, elem):self._data.append(elem)self.length += 1
def remove(self, elem):self._data.remove(elem)self.length -= 1
mylist = MyList()mylist.add(1)mylist.add(2)mylist.add(3)print(mylist.length) # 3mylist.remove(3)print(mylist.length) # 2
items = []items.append("apple")items.append("orange")items.append("banana")
counter = 0for i in items:counter = counter + 1
print(counter)
输出:
3
方法3:使用length_hint()
items = []items.append("apple")items.append("orange")items.append("banana")
from operator import length_hintlist_len_hint = length_hint(items)print(list_len_hint)
输出:
3
性能分析-朴素vslen() vslength_hint()
说明:为了进行比较,我将输入列表更改为一个大集合,可以提供大量的时间差来比较方法。
items = list(range(100000000))
# Performance Analysisfrom operator import length_hintimport time
# Finding length of list# using loop# Initializing counter
start_time_naive = time.time()counter = 0for i in items:# incrementing countercounter = counter + 1end_time_naive = str(time.time() - start_time_naive)
# Finding length of list# using len()start_time_len = time.time()list_len = len(items)end_time_len = str(time.time() - start_time_len)
# Finding length of list# using length_hint()start_time_hint = time.time()list_len_hint = length_hint(items)end_time_hint = str(time.time() - start_time_hint)
# Printing Times of eachprint("Time taken using naive method is : " + end_time_naive)print("Time taken using len() is : " + end_time_len)print("Time taken using length_hint() is : " + end_time_hint)
输出:
Time taken using naive method is : 7.536813735961914Time taken using len() is : 0.0Time taken using length_hint() is : 0.0
#Finding length of list by using len() methodlist_example = [1, 2, 3, 4, 5,6,7,8,9]print("The length of list is: ", len(list_example))The output will be:
Output:The length of list is: 9
朴素方法
使用for循环可以很容易地找到列表的长度,这种方法称为朴素方法。它可以总结如下:
首先,声明一个计数器变量并将其初始化为零。
使用for循环,遍历所有数据元素,在遇到每个元素后,将计数器变量增加1。
现在,数组的长度将存储在计数器变量中,它将表示列表中元素的数量。
#Finding length of list by using Naive method#Initializing listlist_example = [1, 2, 3, 4, 5,6,7,8,9]print("The list is: ", str(list_example))#finding length of list using loopcounter = 0for i in list_example:counter = counter + 1print ("Length of list using Naive Method is: ", str(counter))
#Code for Performance Analysisfrom operator import length_hintimport time#define listlist_example = [1, 2, 3, 4, 5,6,7,8,9]print("The list is: ", list_example)# Using Naive method & loop to find length of list# Initializing counterstart_naive_time = time.time()counter = 0for i in list_example:counter = counter + 1end_naive_time = float(time.time() - start_naive_time)# Using len() methodstart_len_time = time.time()list_len = len(list_example)end_len_time = float(time.time()- start_len_time)# Using length_hint() methodstart_hint_time = time.time()list_hint = length_hint(list_example)end_hint_time = float(time.time()- start_hint_time)#Printing time for each methodprint("Time taken by Naive Method: ", end_naive_time)print("Time taken by Length_hint() Method: ", end_len_time)print("Time taken by len() Method: ", end_hint_time)The output will be this:
Output:
The list is: [1, 2, 3, 4, 5, 6, 7, 8, 9]Time taken by Naive Method: 3.0994415283203125e-06Time taken by Length_hint() Method: 4.76837158203125e-07Time taken by len() Method: 1.1920928955078125e-06