Reference site: [Introduction to Python] How to get the size of a list with list size
When working with lists in Python, you may want to know the size (length) of the list. In Python, the length of the list can be easily obtained by using a function. This time, I will explain how to get the size of a one-dimensional list and a multidimensional list.
table of contents 1 [Get the size of the list with the for statement](Get the size of the list with the ## for statement) 2 [Get the list size with the len function](## Get the list size with the len function) 3 [Get the size of the multidimensional list with the len function](## Get the size of the multidimensional list with the len function) 4 [Get the number of elements specified by the count function](## Get the number of elements specified by the count function)
The most orthodox way to get the size of a list is with a for statement.
list1 = ['python', 'list', 'length']
length = 0
for x in list1:
length += 1
print('The length of list1 is{}is'.format(length))
Execution result
list1 is 3 in length
Since the Python for statement can extract elements from the list one by one, prepare a counter variable (length in this example) and add 1 to the value of the counter variable each time to increase the size of the list. You can get it.
Earlier I explained how to get the size of the list by turning the for statement. You can get the size that way, but Python makes it easier to get the length. That's how to use the len function. The syntax of the len function is as follows.
variable= len(list)
The len function is a function that returns the length of the list passed as an argument. You can get the length of the list in just one line by using len.
list1 = ['python', 'list', 'length']
length = len(list1)
print('The length of list1 is{}is'.format(length))
Execution result
list1 is 3 in length
The source code is very refreshing. The method of using the for statement is not particularly complicated, but it is still cleaner to use the len function.
Earlier we got the size of a one-dimensional list, but sometimes we want to get the size of a two-dimensional or larger multidimensional list. In the case of 1D, there was no problem using the for statement, but in the case of a multidimensional list, trying to retrieve it with just the for statement would be quite complicated.
To get the size of a multidimensional list, combine the len function with the for statement. It's a bit more complicated than 1D, but you can still get the size with a relatively small amount of code by using len.
This time, as an example, we will get the length of the following two-dimensional list.
list1 = [['python', 'list'], ['length', 'two', ' demension']]
First, let's apply the len function to this list normally.
list1 = [['python', 'list'], ['length', 'two', ' demension']]
length = len(list1)
print('The length of list1 is{}is'.format(length))
Execution result
list1 is 2 in length
This way the size is 2. This is because list1 is regarded as a one-dimensional list with two elements, ['python', ‘list’] and [‘length’, ‘two’, ‘demension’].
To get the size of the 2D list correctly, you need to get the size of each element and add them together. In other words, you can get the total size by passing each element to the len function and adding the results together.
list1 = [['python', 'list'], ['length', 'two', ' demension']]
length = 0
for x in list1:
length += len(x) #Add the lengths of the extracted elements
print('The length of list1 is{}is'.format(length))
Execution result
list1 is 5 in length
You have now successfully obtained the overall size. For 3D and above, you can basically get the overall size by using len in the for statement to get the size of each element.
How to get the size of the list depends on the programming language, but some languages get it with a function called "count". In Python, the count function can get "the number of specific elements you specify" instead of the length of the list. Count is also a frequently used function, so it's a good idea to take this opportunity to learn how to use it. The syntax of count is as follows.
Variable = list.count (object)
The count function returns how many objects specified in the argument are in the list.
ist2 = [1, 3, 4, 5, 1, 2, 1]
count_one = list2.count(1)
print('1 in list2{}There are'.format(count_one))
Execution result
There are 3 1s in list2
By the way, if the specified object is not in the list, it will automatically return 0.
list2 = [1, 3, 4, 5, 1, 2, 1]
count_one = list2.count(6)
print('6 in list2{}There are'.format(count_one))
Execution result
There are 0 1s in list2
Recommended Posts