python 中如何查找列表长度

2022-01-09 11:58:54 浏览数 (1)

In this article, we will be unveiling techniques to find the length of a Python list. Finding the length actually means fetching the count of data elements in an iterable.

how to get list length in python

在本文中,我们将揭示找到Python列表长度的技术。 找到长度实际上意味着以可迭代的方式获取数据元素的数量。

技术1:len()方法在Python中查找列表的长度 (Technique 1: The len() method to find the length of a list in Python)

Python has got in-built method — len() to find the size of the list i.e. the length of the list.

Python有内置方法len()来查找列表大小,即列表的长度。

The len() method accepts an iterable as an argument and it counts and returns the number of elements present in the list.

len() method接受iterable作为参数,并计数并返回列表中存在的元素数。

Syntax:

句法:

len(list)

Example:

例:

inp_lst = 'Python','Java','Kotlin','Machine Learning','Keras'size = len(inp_lst)print(size)

Output:

输出:

5

技术2:使用Python for循环获取长度 (Technique 2: Using Python for loop to get the length)

In order to find the length of the list in Python, using for loop is considered as a traditional technique or a naive method in the following manner:

为了在Python中找到列表的长度,使用for循环被认为是一种传统技术,或者通过以下方式作为一种朴素的方法:

  • Declare a counter variable and initialize it to zero. 声明一个计数器变量并将其初始化为零。
  • Using a for loop, traverse through all the data elements and after encountering every element, increment the counter variable by 1. 使用for循环,遍历所有数据元素,遇到每个元素后,将计数器变量加1。
  • Thus, the length of the array will be stored in the counter variable as the variable will represent the number of elements in the list. 因此,数组的长度将存储在计数器变量中,因为该变量将表示列表中元素的数量。

counter = 0for item in list: counter =1print(counter)

Example:

例:

inp_lst = 'Python','Java','Kotlin','Machine Learning','Keras'size = 0print("Length of the input string:")for x in inp_lst: size =1print(size)

Output:

输出:

Length of the input string:5

技术3:length_hint()函数获取列表的长度 (Technique 3: The length_hint() function to get the length of the list)

Python operator module has in-built length_hint() function to calculate the total number of elements in the list.

Python运算符模块具有内置的length_hint()函数,用于计算列表中的元素总数。

Syntax:

句法:

length_hint(iterable)

Example:

例:

from operator import length_hint inp_lst = 'Python','Java','Kotlin','Machine Learning','Keras'print("Length of the input string:")size = length_hint(inp_lst)print(size)

Output:

输出:

Length of the input string:5

查找Python列表长度的最佳方法 (Best approach to find length of a Python list)

Out of all the methods mentioned above, Python in-built len() method is considered as the best approach by programmers to get the size of the list.

在上述所有方法中, Python内置的len()方法被程序员视为获取列表大小的最佳方法。

Reason: The len() function requires O(1) time to calculate the size of the list because as list actually is an object so thus, it has memory space available to store the size.

原因: len() function需要O(1) time来计算列表的大小,因为列表实际上是一个对象,因此,它具有可用于存储大小的内存空间。

结论 (Conclusion)

Thus, in this article, we have understood the different ways to calculate the length of a Python list.

因此,在本文中,我们了解了计算Python列表长度的不同方法。

0 人点赞