Introduction to Python Basics Lesson 8 - Lists

    1 Introduction

    In the content of the sequence in the previous chapter, in fact, we have used the list many times, you can go back and have a look. Some powerful methods and functions are worthy of our in-depth understanding of it. The previous chapters have only introduced some simple usages, and in some examples there are also knowledge of infiltrating lists. In this chapter, we will introduce some very useful and practical methods about lists in detail.

    The most important thing we must know first: lists are mutable - we can change the content of the list, and we can easily change its content to meet our needs through some of the methods described below.

Note: The python code generated in the HTML/XML editor will have a tag language similar to <span style= "color:#333333;" >, this part is not in the code, please ignore it automatically.

    2. Basic list operations

     One: list function

    What does the list function do here? From its name, we can guess that it will help us generate a list, which is equivalent to the initialization of the list. But it should be noted that the list function itself is a type rather than a function. When we use the list function, we do not distinguish this point, which has no effect on our program writing. And the list function works for all types of sequences, not just strings, take a look at what it does.
>>> list('Hello')
['H', 'e', 'l', 'l', 'o']
>>> list('Hello,MyWorld!')
['H', 'e', 'l', 'l', 'o', ',', 'M', 'y', 'W', 'o', 'r', 'l', 'd', '!']

    Two: element assignment

    Remember the index we talked about before, but now it comes in handy. Instead of using the ordinary assignment statement to assign values ​​to the elements in the list as before, we can use the subscripts unique to the elements, that is, the indexes, to operate. Let's take a look.

>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> numbers[2]=100
>>> numbers[::]
[1, 2, 100, 4, 5, 6, 7, 8, 9, 10]
    It should be noted here: you cannot assign an element to an element whose position does not exist in the list. If the length of the list is only 99, you cannot assign an element with an index of 100. If you want to assign a value, then you must create a list of length 101.

    Three: delete element

    To remove an element from a list we need to use the del statement to do it, let's see.
>>> numbers
[1, 2, 4, 5, 6, 7, 8, 9, 10]
>>> len(numbers)
9
>>> del numbers[5]
>>> numbers
[1, 2, 4, 5, 6, 8, 9, 10]
>>> len(numbers)
8
    In fact, the del statement is not only used here, but also used in the subsequent dictionary elements and even the deletion of other variables. We will introduce them one by one in the following chapters, don't worry.

    Four: Fragment assignment

    Short oil, I encountered the word fragmentation again. A very powerful word is accompanied by its powerful functions. Take a look.
>>> numbers
[1, 2, 4, 5, 6, 8, 9, 10]
>>> numbers[5:]=[101,102,103]
>>> numbers
[1, 2, 4, 5, 6, 101, 102, 103]
>>> name=list('youzi')
>>> name
['y', 'o', 'u', 'z', 'i']
>>> name[1:]=list('uanzi')
>>> name
['y', 'u', 'a', 'n', 'z', 'i']
>>> numbers
[1, 2, 4, 5, 6, 101, 102, 103]
>>> numbers[5:7]=[] #A new operation to delete elements in the list
>>> numbers
[1, 2, 4, 5, 6, 103]

    3. List method

    We have introduced the concept of functions before, and we have also experienced some of the more powerful functions of functions in the previous examples. Now we introduce a concept closely related to functions - methods.
    A method is a function that is closely related to some objects. The object may be a list, a number, a string or other types of objects. We have to keep in mind the calling form of the method: object. method (parameter).

    append method

    The append method can be used to add or add new objects or elements at the end of the list according to its meaning, and take a look at the specific usage.
>>> numbers=[1,2,3,4,5,6,7]
>>> numbers
[1, 2, 3, 4, 5, 6, 7]
>>> numbers.append(8)
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8]
    Note: The append method is similar to the other methods, except that it modifies the original list in place, it doesn't simply return a new modified list -- it modifies the original list directly.

    count method

    The count method is used to count the number of times an element appears in the list:

>>> numbers.append(5)
>>> numbers
[1, 2, 3, 5, 5]
>>> numbers.count(5)
2

    extend method

    The extend method can append multiple values ​​in another sequence to the end of the list at one time, and can use the new list to extend the original list. come and see:
>>> newnumbers=[6,7,8,9,10]
>>> numbers.extend(newnumbers)
>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9, 10]
    Do you think of the general string concatenation for such a result? Let's see what effect it has:
>>> numbers=[1,2,3,5,5]
>>> newnumbers
[6, 7, 8, 9, 10]
>>> numbers+newnumbers# stitching stitching
[1, 2, 3, 5, 5, 6, 7, 8, 9, 10]
>>> numbers
[1, 2, 3, 5, 5]
>>> numbers[len(numbers):]=newnumbers
>>> numbers #Compare the numbers here and the numbers after splicing above what is the difference
[1, 2, 3, 5, 5, 6, 7, 8, 9, 10]

    index method

    The index method is used to find the index position of the first match of a value from the list.

>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9, 10]
>>> numbers.index(3)
2
>>> numbers.index(10)
9
>>> numbers[9]
10
    Here, the element corresponding to the position with the subscript 9 is 10, then the element 10 is definitely displayed when looking for the element through the index 9.

    insert method

    The insert method is used to insert objects into the list.
>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9, 10]
>>> numbers.insert(4,11)
>>> numbers
[1, 2, 3, 5, 11, 5, 6, 7, 8, 9, 10]

    pop method

    The pop method removes an element from the list (the last one by default) and returns the value of that element. The pop method is the only method that can both modify a list and return a list of element values!
   
>>> numbers
[1, 2, 3, 5, 11, 5, 6, 7, 8, 9, 10]
>>> numbers.pop()
10
>>> numbers
[1, 2, 3, 5, 11, 5, 6, 7, 8, 9]
>>> numbers.pop(4)
11
>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9]

    remove method

    The remove method is used to remove the first occurrence of a value in the list.
    
>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9]
>>> numbers
[1, 2, 3, 5, 5, 6, 7, 8, 9]
>>> numbers.remove(5)
>>> numbers
[1, 2, 3, 5, 6, 7, 8, 9]

    sort method

    The name of this method sounds like sorting, but it uses a fixed sorting method, which means changing the original list so that the elements in it can be sorted in a certain order, rather than simply returning a sorted copy.
>>> numbers
[1, 2, 3, 5, 100, 0, 6, 7, 8, 9, 88]
>>> numbers.sort()
>>> numbers
[0, 1, 2, 3, 5, 6, 7, 8, 9, 88, 100]
>>> new=numbers.sort()
>>> print new
None
    If you really want to do this, you can do it this way, which is equivalent to creating a new copy of number.
>>> numbers=[100,2,5,8,3,0,99]
>>> new=numbers[:]
>>> new.sort()
>>> new
[0, 2, 3, 5, 8, 99, 100]

    4. Advanced sorting methods

    If you want the elements to be sorted in a specific way, instead of the default sorting method of sort, you can customize the comparison function in the form of compare(x,y). The compare function will return a negative number when x<y and a positive number when x>y. If x=y, return 0. After defining this function, you can provide parameters to the sort function.
There are also two optional parameters of the sort function, key and reverse. If you want to use them, you must specify them by name. The parameter key, like the parameter cmp, must provide a function used in the sorting process, but the function is not directly used to determine the size of the object, but creates a key for each element, and then all elements are sorted according to the key. Take a look at the following example:
>>> x=[4,6,2,1,7,9]
>>> x
[4, 6, 2, 1, 7, 9]
>>> x.sort(reverse=True)
>>> x
[9, 7, 6, 4, 2, 1]
>>> x=['aardvark','abalone','acme','add','aerate']
>>> x.sort(key=len)
>>> x
['add', 'acme', 'aerate', 'abalone', 'aardvark']
    Well, the sequence part will be introduced here first, and hands-on practice is one of the best ways to learn. Next chapter preview -- tuples.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325776288&siteId=291194637