欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何在Python中获取列表的长度和大小?

程序员文章站 2022-03-04 20:09:58
...

Python is a very expressive language that provides different structures to easy developers work. The list is one of the most popular data structures provided by the python. In a regular workflow, we add and remove elements into and from the list. But in this floating situations, we need to get the length of the list. How can we get the length or size of the list? In this tutorial, we will look at different ways to get a list of length.

Python是一种非常具有表现力的语言,它提供了不同的结构来简化开发人员的工作。 该列表是python提供的最受欢迎的数据结构之一。 在常规工作流程中,我们在列表中添加元素或从列表中删除元素。 但是在这种浮动的情况下,我们需要获取列表的长度。 我们如何获得列表的长度或大小? 在本教程中,我们将研究获取长度列表的不同方法。

使用内置的len()函数 (Using Built-in len() Function)

As we stated before len is a builtin function provided python by default. We can use this function just providing the list as an argument like below. The syntax is very simple we will just provide the list, array, tuple or dictionary type variable into the len() function as an array.

如前所述, len是默认情况下python提供的内置函数。 我们可以使用此功能,仅将列表作为参数提供,如下所示。 语法非常简单,我们仅将len() ,数组中的列表,数组,元组或字典类型变量提供为数组。

name_list=['ismail','ahmet','ali']
len(name_list)


fruit_list = ['apple', 'banana', 'carrot' , 'melon' , 'tomato']
len(fruit_list)


number_list = [1,2,3,4,5,6,7,8,9,10,12,13,14,15]
len(number_list)
如何在Python中获取列表的长度和大小?
Using Built-in len() Function
使用内置的len()函数

获取多维列表长度(Get Multi Dimension List Length)

In previous we have looked at the length of a single dimension list. But in real-world situations, there will be multi-dimension lists. We can also get the length of this list single dimension length just providing the index of the related sub-list like below. In this example, we want to get the length of the first sub-array.

在前面的内容中,我们研究了单个维度列表的长度。 但是在现实情况下,将存在多维列表。 我们还可以仅通过提供相关子列表的索引来获得此列表一维长度的长度,如下所示。 在此示例中,我们要获取第一个子数组的长度。

name_list=[['ismail','elif'],'ahmet','ali',[7,8,9,10],1,2,3,['apple', 'banana', 'carrot' , 'melon' , 'tomato']]
len(name_list)
len(name_list[0])
len(name_list[7])
如何在Python中获取列表的长度和大小?
Get Multi Dimension List Length
获取多维列表长度

We provide the sublist element index as  which is ['ismail','elif'] and get the length of this sub-list as  2

我们提供子列表元素索引为这是['ismail','elif']并将此子列表的长度设为2

使用For循环通过迭代每个元素来计算长度 (Count Length with For Loop By Iterating Each Element)

len() function provides a very convenient, easy, and efficient way to get the length or size of an array. But in some cases, we may want to calculate a list length or size by counting it one by one. Even we want to eliminate some elements in a list and do not count them. In this example, we can achieve this by using for loop with a list.

len()函数提供了一种非常方便,轻松且有效的方式来获取数组的长度或大小。 但是在某些情况下,我们可能希望通过一一计算列表的长度或大小。 甚至我们也想消除列表中的某些元素并且不计算它们。 在此示例中,我们可以通过for列表使用for循环来实现。

name_list=['ismail','ahmet','ali']
count=0
for element in name_list:
   count=count+1
print(count)




name_list=[['ismail','elif'],'ahmet','ali',[7,8,9,10],1,2,3,['apple', 'banana', 'carrot' , 'melon' , 'tomato']]
count=0
for element in name_list:
   count=count+1
print(count)
如何在Python中获取列表的长度和大小?
Count Length with For Loop By Iterating Each Element
使用For循环通过迭代每个元素来计算长度

字典长度(Dictionary Length)

len() function is very useful where it can be used to get the length or size of different array types like the dictionary. We can use the same syntax to count dictionary key, value type elements. This will count 1 a single key and value pair.

len()函数在获取诸如字典之类的不同数组类型的长度或大小时非常有用。 我们可以使用相同的语法来计算字典键,值类型元素。 这将对1个键和值对进行计数。

name_surname={'ismail':'baydan','ahmet':'baydan','ali':'baydan'}

len(name_surname)
如何在Python中获取列表的长度和大小?
Dictionary Length
字典长度
了解更多如何在Python中对列表进行排序?

翻译自: https://www.poftut.com/get-length-size-list-python/