python3 列表长度_3种在Python中查找列表长度的简便方法

本文介绍了三种在Python中查找列表长度的方法:1) 使用内置的len()函数,这是最常用且效率高的方法;2) 通过for循环遍历计算长度,适合初学者理解;3) 利用length_hint()函数,适用于特定情况。程序员通常推荐len()方法,因为它直接从列表对象获取长度信息。

python3 列表长度

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.

在本文中,我们将揭示找到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 = 0
for item in list:
 counter+=1
print(counter)

Example:

例:


inp_lst = ['Python','Java','Kotlin','Machine Learning','Keras']
size = 0
print("Length of the input string:")
for x in inp_lst:
    size+=1
print(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()函数,用于计算列表中的元素总数。

The operator.length_hint() method is used to find the length of an iterable such as list, tuple, dict, etc.

operator.length_hint()方法用于查找可迭代对象的长度,例如list, tupledict等。

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列表长度的不同方法。



参考资料 (References)

翻译自: https://www.journaldev.com/37856/find-the-length-of-a-list-in-python

python3 列表长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值