📜  Python|查找列表长度的方法

📅  最后修改于: 2022-05-13 01:54:55.705000             🧑  作者: Mango

Python|查找列表长度的方法

List 是Python日常编程不可或缺的一部分,所有Python用户都必须学习,并且了解它的实用程序和操作是必不可少的,而且总是一个加分项。因此,本文讨论了一种这样的查找号码的实用程序。列表中的元素。

方法一:朴素方法

在这种方法中,只需运行一个循环并增加计数器,直到列表的最后一个元素知道它的计数。这是在没有其他现有技术的情况下可能采用的最基本的策略。
代码 #1:演示使用 Naive 方法查找列表的长度

Python3
# Python code to demonstrate
# length of list
# using naive method
 
# Initializing list
test_list = [ 1, 4, 5, 7, 8 ]
 
# Printing test_list
print ("The list is : " + str(test_list))
 
# Finding length of list
# using loop
# Initializing counter
counter = 0
for i in test_list:
     
    # incrementing counter
    counter = counter + 1
 
# Printing length of list
print ("Length of list using naive method is : " + str(counter))


Python3
# Python program to demonstrate working
# of len()
a = []
a.append("Hello")
a.append("Geeks")
a.append("For")
a.append("Geeks")
print("The length of list is: ", len(a))


Python3
# Python program to demonstrate working
# of len()
n = len([10, 20, 30])
print("The length of list is: ", n)


Python3
# Python code to demonstrate
# length of list
# using len() and length_hint
from operator import length_hint
 
# Initializing list
test_list = [ 1, 4, 5, 7, 8 ]
 
# Printing test_list
print ("The list is : " + str(test_list))
 
# Finding length of list
# using len()
list_len = len(test_list)
 
# Finding length of list
# using length_hint()
list_len_hint = length_hint(test_list)
 
# Printing length of list
print ("Length of list using len() is : " + str(list_len))
print ("Length of list using length_hint() is : " + str(list_len_hint))


Python3
# Python code to demonstrate
# length of list
# Performance Analysis
from operator import length_hint
import time
 
# Initializing list
test_list = [ 1, 4, 5, 7, 8 ]
 
# Printing test_list
print ("The list is : " + str(test_list))
 
# Finding length of list
# using loop
# Initializing counter
start_time_naive = time.time()
counter = 0
for i in test_list:
     
    # incrementing counter
    counter = counter + 1
end_time_naive = str(time.time() - start_time_naive)
 
# Finding length of list
# using len()
start_time_len = time.time()
list_len = len(test_list)
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(test_list)
end_time_hint = str(time.time() - start_time_hint)
 
# Printing Times of each
print ("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)


输出 :

The list is : [1, 4, 5, 7, 8]
Length of list using naive method is : 5

方法 2:使用 len()

len() 方法提供了最常用和最简单的方法来查找任何列表的长度。这是当今所有程序员采用的最传统的技术。

Python3

# Python program to demonstrate working
# of len()
a = []
a.append("Hello")
a.append("Geeks")
a.append("For")
a.append("Geeks")
print("The length of list is: ", len(a))
输出:
The length of list is:  4

Python3

# Python program to demonstrate working
# of len()
n = len([10, 20, 30])
print("The length of list is: ", n)
输出:
The length of list is:  3

方法 3:使用 length_hint()

这种技术是一种鲜为人知的查找列表长度的技术。这个特定的方法是在运算符类中定义的,它也可以判断为否。列表中存在的元素。
代码 #2:演示使用 len() 和 length_hint() 查找列表长度

Python3

# Python code to demonstrate
# length of list
# using len() and length_hint
from operator import length_hint
 
# Initializing list
test_list = [ 1, 4, 5, 7, 8 ]
 
# Printing test_list
print ("The list is : " + str(test_list))
 
# Finding length of list
# using len()
list_len = len(test_list)
 
# Finding length of list
# using length_hint()
list_len_hint = length_hint(test_list)
 
# Printing length of list
print ("Length of list using len() is : " + str(list_len))
print ("Length of list using length_hint() is : " + str(list_len_hint))

输出 :

The list is : [1, 4, 5, 7, 8]
Length of list using len() is : 5
Length of list using length_hint() is : 5

性能分析——Naive vs len() vs length_hint()

当在备选方案中进行选择时,总是有必要有一个合理的理由来选择一个而不是另一个。本节对执行所有这些需要多少时间进行时间分析,以提供更好的使用选择。
代码 #3:性能分析

Python3

# Python code to demonstrate
# length of list
# Performance Analysis
from operator import length_hint
import time
 
# Initializing list
test_list = [ 1, 4, 5, 7, 8 ]
 
# Printing test_list
print ("The list is : " + str(test_list))
 
# Finding length of list
# using loop
# Initializing counter
start_time_naive = time.time()
counter = 0
for i in test_list:
     
    # incrementing counter
    counter = counter + 1
end_time_naive = str(time.time() - start_time_naive)
 
# Finding length of list
# using len()
start_time_len = time.time()
list_len = len(test_list)
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(test_list)
end_time_hint = str(time.time() - start_time_hint)
 
# Printing Times of each
print ("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)

输出 :

The list is : [1, 4, 5, 7, 8]
Time taken using naive method is : 2.6226043701171875e-06
Time taken using len() is : 1.1920928955078125e-06
Time taken using length_hint() is : 1.430511474609375e-06

结论:

在下图中,可以清楚地看到所花费的时间是幼稚的 >> length_hint() > len() ,但所花费的时间很大程度上取决于操作系统及其几个参数。在连续两次运行中,您可能会得到对比鲜明的结果,实际上有时 naive 花费的时间最少是三个。所有可能的 6 种排列都是可能的。

长度提示()>天真>长度(),

天真 > len()=length_hint()

天真>长度提示()>长度()

len > naive > length_hint() > naive