关于linux:如何获取列表长度

63次阅读

共计 2530 个字符,预计需要花费 7 分钟才能阅读完成。

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