current position:Home>From zero to familiarity, it will take you to master the use of Python len() function

From zero to familiarity, it will take you to master the use of Python len() function

2022-01-30 03:02:49 Huawei cloud developer community

Abstract : This article shows you how to find the use of length built-in data types len() Use len() With third party data types Provide for support len() With user-defined classes .

This article is shared from Huawei cloud community 《 stay Python Use in len() function 》, author :Yuchuan .

in many instances , You need to find the number of items stored in the data structure .Python Built in functions for len() Is a tool to help you complete this task .

In some cases , Use len() It's simple . however , Sometimes you need to understand in more detail how this function works and how it applies to different data types .

In this tutorial , You will learn how to :

  • The length found is the use of built-in data types len()
  • Use len() With third party data types
  • Provide for support len() With user-defined classes

At the end of this article , You will know when to use len()Python Function and how to use it effectively . You will know which built-in data types are valid parameters len(), Which cannot be used . You can also learn how to use len() Third party type , Such as ndarray With the NumPy and DataFrame In the giant panda , And use your own class .

Python introduction len()

This function len() yes Python One of the built-in functions of . It returns the length of the object . for example , It can return the number of items in the list . You can use this function for many different data types . however , Not all data types are Valid parameters for len().

You can start by viewing help for this feature :

>>>
>>> help(len)
Help on built-in function len in module builtins:
len(obj, /)
    Return the number of items in a container.
 Copy code 

This function takes an object as a parameter and returns the length of the object . This document len() Go away :

Returns the length of the object ( Number of projects ). Parameters can be sequences ( Like strings 、 byte 、 Tuples 、 List or range ) Or set ( For example, a dictionary 、 Set or freeze set ).( source )

When you use built-in data types and many with Third party type of len(), This function does not need to traverse the data structure . The length of the container object is stored as an attribute of the object . Every time you add or delete an item in the data structure , Will modify this property len() Value , And return the value of the length property . This ensures that the len() Work effectively .

In the following sections , You will learn how to use len() Sequences and sets . You will also learn about some that cannot be used as len()Python The data type of the function parameter .

Use len() Built in sequence

One Sequence Is the container for ordering items . list 、 Tuples and strings are Python Three basic built-in sequences in . You can find the length of the sequence by calling len():

>>>
>>> greeting = "Good Day!"
>>> len(greeting)
9

>>> office_days = ["Tuesday", "Thursday", "Friday"]
>>> len(office_days)
3

>>> london_coordinates = (51.50722, -0.1275)
>>> len(london_coordinates)
2
 Copy code 

Looking for string greeting、 listoffice_days and tuple When the length of the london_coordinates, you len() Use... In the same way . All three data types are Valid parameters for len().

This function len() Always return an integer , Because it is counting the number of items in the object you pass to it .0 If the parameter is an empty sequence , Then the function returns :

>>>
>>> len("")
0
>>> len([])
0
>>> len(())
0
 Copy code 

In the example above , You found an empty string 、 The length of an empty list and an empty tuple . This function 0 In each case, return .

One range Objects can also be created using sequences range(). One range Object does not store all values , But they arise when needed . however , You can still range Use len() Find the length of the object in the following way :

>>>
>>> len(range(1, 20, 2))
10
 Copy code 

This number ranges from 1 To 19 Integer in increments 2.range The length of the object can be determined by starting 、 Stop and step values to determine .

In this section , You have len()Python The function is used for string 、 list 、 Tuples and range object . however , You can also use this function with any other built-in sequence .

Use len() With built-in collection

At some point , You may need to find the number of unique items in a list or other sequence . You can use sets and len() To achieve this :

>>>
>>> import random

>>> numbers = [random.randint(1, 20) for _ in range(20)]

>>> numbers
[3, 8, 19, 1, 17, 14, 6, 19, 14, 7, 6, 1, 17, 10, 8, 14, 17, 10, 2, 5]

>>> unique_numbers = set(numbers)
{1, 2, 3, 5, 6, 7, 8, 10, 14, 17, 19}

>>> len(unique_numbers)
11
 Copy code 

you numbers Use list derivation to generate a list , It contains 20 Between 1 A random number between and 20. Because you generate random numbers , Therefore, the output will be different every time you run the code . In this particular operation , Yes 20 There are... In a randomly generated list of numbers 11 A unique number .

Another built-in data type that you will often use is dictionary. In the dictionary , Each item consists of a key value pair . When you use a dictionary as Parameter time len(), This function returns the number of items in the dictionary :

>>>
>>> len({"James": 10, "Mary": 12, "Robert": 11})
3

>>> len({})
0
 Copy code 

The output of the first example shows that there are three key value pairs in this dictionary . As in the case of sequences , When the parameter is an empty dictionary or empty set len() Will return 0. This causes empty dictionaries and empty sets to be false .

Explore len() Other built-in data types

You cannot use all built-in data types as len(). For data types where multiple items are not stored , The concept of length is irrelevant . This is the case of numbers and Boolean types :

>>>
>>> len(5)
Traceback (most recent call last):
    ...
TypeError: object of type 'int' has no len()

>>> len(5.5)
Traceback (most recent call last):
     ...
TypeError: object of type 'float' has no len()

>>> len(True)
Traceback (most recent call last):
     ...
TypeError: object of type 'bool' has no len()

>>> len(5 + 2j)
Traceback (most recent call last):
     ...
TypeError: object of type 'complex' has no len()
 Copy code 

The integer , Floating point numbers , Boolean , And built-in data types of complex types , You can't use examples len().TypeError When the parameter is an object of data type without length , This function raises a .

You can also explore whether you can use iterators and generators as parameters len():

>>>
>>> import random

>>> numbers = [random.randint(1, 20) for _ in range(20)]
>>> len(numbers)
20

>>> numbers_iterator = iter(numbers)
>>> len(numbers_iterator)
Traceback (most recent call last):
     ...
TypeError: object of type 'list_iterator' has no len()

>>> numbers_generator = (random.randint(1, 20) for _ in range(20))
>>> len(numbers_generator)
Traceback (most recent call last):
     ...
TypeError: object of type 'generator' has no len()
 Copy code 

You have seen that the list has a length , This means that you can use it as len(). You can use built-in functions to create an iterator from the list iter(). In the iterator , As long as you need , Will get each item , For example, when using functions next() Or in a loop . however , You can't be in len().

You got it. TypeError, When you try to use an iterator as a parameter len(). Because the iterator gets each item when needed , Therefore, the only way to measure its length is to deplete the iterator . Iterators can also be infinite , For example, by Iterator returned itertools.cycle(), So its length cannot be defined .

Do not use generators and len() For the same reason . If you don't use them , You can't measure the length of these objects .

len() Use some examples to further explore

In this section , You will learn len(). These examples will help you better understand when to use this feature and how to use it effectively . In some examples , You will also see len() Possible solutions, but there may be more Pythonic Method to achieve the same output .

Verify the length entered by the user

A common use case for len() Is the length of the sequence entered by the user :

# username.py

username = input("Choose a username: [4-10 characters] ")

if 4 <= len(username) <= 10:
    print(f"Thank you. The username {username} is valid")
else:
    print("The username must be between 4 and 10 characters long")
 Copy code 

In this example , You use if Statement to check Returned integer len() Is greater than or equal to 4 And less than or equal to 10. You can run this script , You will get an output similar to the following :

$ python username.py
Choose a username: [4-10 characters] stephen_g
Thank you. The username stephen_g is valid
 Copy code 

under these circumstances , The length of the user name is 9 Characters , therefore if The condition in the statement evaluates to True. You can run the script again and enter an invalid user name :

$ python username.py
Choose a username: [4-10 characters] sg
The username must be between 4 and 10 characters long
 Copy code 

under these circumstances ,len(username) return 2, also if The condition in the statement evaluates to False.

End the loop according to the length of the object

len() If you need to check the variable sequence ( For example, a list of ) When does the length of reach a certain number , You will use . In the following example , You ask the user to enter three user name options , And store them in the list :

# username.py

usernames = []

print("Enter three options for your username")

while len(usernames) < 3:
    username = input("Choose a username: [4-10 characters] ")
    if 4 <= len(username) <= 10:
        print(f"Thank you. The username {username} is valid")
        usernames.append(username)
    else:
        print("The username must be between 4 and 10 characters long")

print(usernames)
 Copy code 

The results you are using now len() Of while Statement . If the user enters an invalid user name , You will not keep the input . When the user enters a valid string , You attach it to the list usernames. Cycle over and over , Until there are three items in the list .

You can even len() Used to check when the sequence is empty :

>>>
>>> colors = ["red", "green", "blue", "yellow", "pink"]

>>> while len(colors) > 0:
...     print(f"The next color is {colors.pop(0)}")
...
The next color is red
The next color is green
The next color is blue
The next color is yellow
The next color is pink
 Copy code 

You use list Method .pop() Remove the first item from the list in each iteration , Until the list is empty . If you use this method on a large list , The item... Should be deleted from the end of the list , Because it's more efficient . You can also use the in the built-in module deque data type collections, It allows you to effectively pop up from the left .

By using the authenticity of the sequence , There is a more Pythonic To achieve the same output :

>>>
>>> colors = ["red", "green", "blue", "yellow", "pink"]

>>> while colors:
...    print(f"The next color is {colors.pop(0)}")
...
The next color is red
The next color is green
The next color is blue
The next color is yellow
The next color is pink
 Copy code 

Empty list is false . This means that it's time to while Statement interprets an empty list as False. Non empty lists are real ,while The statement treats it as True. The value returned len() Determines the authenticity of the sequence . One sequence is truthy When len() Returns any non-zero integer , And when the falsylen() return 0.

Find the index of the last item in the sequence

Imagine , You want to generate a range 1 Is a sequence of random numbers ,10 And you want to keep adding numbers to the sequence , Until the sum of all the numbers exceeds 21. The following code creates an empty list and uses while Cycle through the list :

>>>
>>> import random

>>> numbers = []
>>> while sum(numbers) <= 21:
...    numbers.append(random.randint(1, 10))

>>> numbers
[3, 10, 4, 7]

>>> numbers[len(numbers) - 1]
7

>>> numbers[-1]  # A more Pythonic way to retrieve the last item
7

>>> numbers.pop(len(numbers) - 1)  # You can use numbers.pop(-1)
7

>>> numbers
[3, 10, 4]
 Copy code 

You append random numbers to the list , Until the sum exceeds 21. When you generate random numbers , The output you will get will be different . To display the last number in the list , Please use it len(numbers) and 1 Subtract... From it , Because the first index of the list is 0.Python Indexes in allow you to use indexes -1 To get the last item in the list . therefore , Although you can len() Use in this case , But you don't need .

You want to delete the last number in the list , So that the sum of all the numbers in the list does not exceed 21. you len() Use again to calculate the index of the last item in the list , You use it as an argument to the list method .pop(). Even in this case , You can also -1 Used as a .pop() Remove the last item from the list and return its parameters .

Split the list in half

If you need to split the sequence in half , You need to use an index that represents the midpoint of the sequence . You can use len() To find this value . In the following example , You will create a list of random numbers , Then split it into two smaller lists :

>>>
>>> import random

>>> numbers = [random.randint(1, 10) for _ in range(10)]
>>> numbers
[9, 1, 1, 2, 8, 10, 8, 6, 8, 5]

>>> first_half = numbers[: len(numbers) // 2]
>>> second_half = numbers[len(numbers) // 2 :]

>>> first_half
[9, 1, 1, 2, 8]
>>> second_half
[10, 8, 6, 8, 5]
 Copy code 

In defining In the assignment statement of first_half, Use to start with numbers Slice the item to the midpoint . You can calculate what the slice represents by decomposing the steps used in the slice expression :

  1. First ,len(numbers) Return integer 10.
  2. Next ,10 // 2 stay 5 Returns an integer when using the integer division operator .
  3. Last ,0:5 It's a slice , Represents the first five items , Its index 0 by 4. Please note that , Endpoints are excluded .

In the next definition second_half, Use the same expression in the slice . however , under these circumstances , Integers 5 Indicates the beginning of the range . Slice now 5: Represents from the index 5 Items to the end of the list .

If your original list contains an odd number of items , Then half its length will no longer be an integer . When you use integer division , You will get the lower limit of the number . The list first_half Now it will be better than One less second_half.

You can create a file that contains 11 A number instead of 10 Try this with an initial list of numbers . The list of results will no longer be half , But they will represent the closest alternative to splitting odd sequences .

take len() Functions are used with third-party libraries

You can also Pythonlen() Use with multiple custom data types from third-party libraries . In the last section of this tutorial , You will learn How do you behave len() Depending on the class definition . In this section , You will see using len() Examples of data types from two popular third-party libraries .

NumPy Of ndarray

The NumPy The module is in Python The cornerstone of all quantitative applications of programming . This module introduces numpy.ndarray data type . This data type and NumPy The functions in are very suitable for numerical calculation , And it is the building block of data types in other modules .

At the beginning of use NumPy Before , You need to install the Library . You can use Python Standard package manager pip, And run the following command in the console :

$ python -m pip install numpy
 Copy code 

You have installed NumPy, Now you can create a... From the list NumPy Array and len() Use... On this array :

>>>
>>> import numpy as np

>>> numbers = np.array([4, 7, 9, 23, 10, 6])
>>> type(numbers)
<class 'numpy.ndarray'>

>>> len(numbers)
6
 Copy code 

NumPy Function from the list you pass as an argument np.array() Create an object of type numpy.ndarray.

however ,NumPy Arrays can have multiple dimensions . You can create a two-dimensional array by converting a list to an array :

>>>
>>> import numpy as np

>>> numbers = [
    [11, 1, 10, 10, 15],
    [14, 9, 16, 4, 4],
]

>>> numbers_array = np.array(numbers)
>>> numbers_array
array([[11,  1, 10, 10, 15],
       [14,  9, 16,  4,  4]])

>>> len(numbers_array)
2

>>> numbers_array.shape
(2, 5)

>>> len(numbers_array.shape)
2

>>> numbers_array.ndim
2
 Copy code 

The list numbers It consists of two lists , Each list contains five integers . When you use this list to create NumPy Array time , The result is an array of two rows and five columns . When you pass this two-dimensional array as a parameter to In the middle of the day , This function returns the number of rows in the array len().

To get the size of two dimensions , You can use properties .shape, It is a tuple showing the number of rows and columns . You can use the .shape and len() Or by using Property to get NumPy The dimensions of an array .ndim.

Generally speaking , When you have an array of arbitrary dimensions ,len() Returns the size of the first dimension :

>>>
>>> import numpy as np

>>> array_3d = np.random.randint(1, 20, [2, 3, 4])
>>> array_3d
array([[[14,  9, 15, 14],
        [17, 11, 10,  5],
        [18,  1,  3, 12]],
       [[ 1,  5,  6, 10],
        [ 6,  3,  1, 12],
        [ 1,  4,  4, 17]]])

>>> array_3d.shape
(2, 3, 4)

>>> len(array_3d)
2
 Copy code 

In this case , You will create a three-dimensional array , Its shape is (2, 3, 4) Each of these elements is 1 A random integer between and 20. This time you use this function np.random.randint() Created an array . function len() return 2, This is the size of the first dimension .

see NumPy course : You are in Python The first step into data science , Learn about using NumPy More information about arrays .

Pandas’ DataFrame

pandas In the library DataFrame Type is another data type widely used in many applications .

In the use of pandas Before , You need to use the following command in the console to install :

$ python -m pip install pandas
 Copy code 

You have installed pandas package , Now you can create a... From the dictionary DataFrame:

>>>
>>> import pandas as pd

>>> marks = {
    "Robert": [60, 75, 90],
    "Mary": [78, 55, 87],
    "Kate": [47, 96, 85],
    "John": [68, 88, 69],
}

>>> marks_df = pd.DataFrame(marks, index=["Physics", "Math", "English"])

>>> marks_df
         Robert  Mary  Kate  John
Physics      60    78    47    68
Math         75    55    96    88
English      90    87    85    69

>>> len(marks_df)
3

>>> marks_df.shape
(3, 4)
 Copy code 

The key of the dictionary is a string representing the names of students in the class . The value of each key is a list , It contains tags for three topics . When you create from this dictionary DataFrame when , You can define an index using a list containing topic names .

DataFrame There are three rows and four columns . This function len() return DataFrame The number of lines in . The DataFrame There's another type .shape attribute , You can use it to display DataFrame The first dimension of represents the number of rows .

You've learned how to len() Use many built-in data types and some data types from third-party modules . In the next section , You will learn how to define any class , So that it can be used as len()Python The parameters of the function .

You can go to The Pandas DataFrame: Make Working With Data Delightful Further exploration in pandas modular .

Use len() User defined classes

When you define a class , One of the special methods you can define is .__len__(). These special methods are called dunder Method , Because they have double underscores at the beginning and end of the method name .Python The built-in len() The function calls its arguments .__len__() Method .

In the last section , You have seen len() When the parameter is a pandasDataFrame Object behavior . This behavior is determined by the class's .__len__() The method decides DataFrame, You can see in the source code of the following modules pandas.core.frame:

class DataFrame(NDFrame, OpsMixin):
    # ...
    def __len__(self) -> int:
        """
        Returns length of info axis, but here we use the index.
        """
        return len(self.index)
 Copy code 

This method uses return DataFrame.index The length of the property len(). this dunder Methods will DataFrame The length of is defined as equal to DataFrame The number of lines in , Such as Shown .index.

You can .__len__() Explore further with the following toy examples dunder Method . You will define a named YString. This data type is based on the built-in string class , But type objects YString Give letters Y More important than all the other letters :

# ystring.py

class YString(str):
    def __init__(self, text):
        super().__init__()

    def __str__(self):
        """Display string as lowercase except for Ys that are uppercase"""
        return self.lower().replace("y", "Y")

    def __len__(self):
        """Returns the number of Ys in the string"""
        return self.lower().count("y")
 Copy code 

.__init__() Method YString Use .__init__() Father str Class . You can do this using functions super(). The .__str__() Method defines how the object is displayed . function str()、print() and format() Call this method . For such , You represent the object as an all lowercase string , But the letters Y With the exception of , It appears in uppercase .

For this toy class , You define the length of the object as the letter in the string Y Number of occurrences of . therefore , The .__len__() Method returns the letter Y Count of .

You can create an object of a class YString And find its length . The module name used for the above example is ystring.py:

>>>
>>> from ystring import YString

>>> message = YString("Real Python? Yes! Start reading today to learn Python")

>>> print(message)
real pYthon? Yes! start reading todaY to learn pYthon

>>> len(message)  # Returns number of Ys in message
4
 Copy code 

you YString Create type objects from type objects str And use Displays the representation of the object print(). Then the object message Used as a Parameters of len(). This will call the... Of the class .__len__() Method , The result is letters Y stay The number of occurrences in message. under these circumstances , Letter Y Four times .

YString This class is not a very useful class , But it helps to illustrate how to customize act len() To meet your needs . The .__len__() Method must return a nonnegative integer . otherwise , It can cause mistakes .

Another special method is .__bool__() Method , It determines how objects are converted to Boolean values . The .__bool__()dunder Methods are not usually used for sequence and set definitions . In these cases , The .__len__() Method to determine the authenticity of the object :

>>>
>>> from ystring import YString

>>> first_test = "tomorrow"
>>> second_test = "today"

>>> bool(first_test)
True

>>> bool(YString(first_test))
False

>>> bool(second_test)
True

>>> bool(YString(second_test))
True
 Copy code 

Variable first_string There is no Y. Such as The output of is shown in bool(), The string is true , Because it's not empty . however , When you YString When creating an object of type from this string , The new object is fake , Because there is no... In the string Y Letter . therefore ,len() return 0. contrary , Variable second_string It does contain letters Y, So string and type objects YString They are all true values. .

You can go to Python 3 Object - oriented programming (OOP) Read more about using object-oriented programming and defining classes .

Conclusion

You've explored how to use len() To determine the sequence 、 Sets and other data types that contain multiple items at the same time ( for example NumPy Array and Pandas DataFrames) Number of items in .

The len()Python Functions are key tools in many programs . Some of its uses are simple , But as you can see in this tutorial , This functionality is much more than the most basic use case . Knowing when you can use this feature and how to use it effectively will help you write cleaner code .

In this tutorial , You learned how to :

  • The length found is the use of built-in data types len()
  • Use len() With third party data types
  • Provide for support len() With user-defined classes

You now understand this len() Function lays a good foundation .len() Learn more about about Can help you better understand the differences between data types . You are ready to len() Use... In your algorithm , And by using .__len__() Method to enhance some class definitions to improve their functionality .

Click to follow , The first time to learn about Huawei's new cloud technology ~

copyright notice
author[Huawei cloud developer community],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300302473045.html

Random recommended