[Edit, test and edit] zero-based learning python_07_ list (modify, add and delete elements)

Most lists you create will be dynamic, which means that after the list is created, elements will be added or deleted as the program runs. For example, you create a game that requires players to shoot aliens that fall from the sky; to do this, you can store some aliens in the list at the beginning, and then remove them from aliens whenever they are shot. Delete from the list, and every time a new alien appears on the screen, it is added to the list. During the entire game, the length of the alien list will continue to change.

Modifying list elements
The syntax of modifying list elements is similar to that of accessing list elements. To modify a list element, specify the list name and the index of the element to be modified, and then specify the new value of the element.
For example, suppose there is a list of motorcycles, the first element of which is'honda', how to modify its value?
We first define a list of motorcycles, the first element of which is'honda'. Next, we change the value of the first element to'ducati'. The output shows that the value of the first element has indeed changed, but the
values of the other elements of the list has not changed:

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles[0] = 'harley'

print(motorcycles)

Copy code

You can modify the value of any list element, not just the value of the first element.

Add an element in a list
you might for a number of reasons to add a new element in the list, for example, you might want a new game aliens appear, add visual data or add new user registered to the site. Python provides a variety of ways to add new data to an existing list.

  1. Adding elements
    at the end of the list When adding new elements to the list, the easiest way is to append the elements to the end of the list. When you append an element to the list, it will be added to the end of the list. Continue using the list from the previous example, adding a new element'ducati' at the end:

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles.append('harley')

print(motorcycles)

Copy code

The append() method adds the element'harley' to the end of the list without affecting all other elements in the list:

The append() method makes it easy to create a list dynamically. For example, you can create an empty list first, and then use a series of append() statements to add elements. Let's create an empty list and add the elements'honda','yamaha' and'suzuki' to it:

motorcycles = []

motorcycles.append('honda')

motorcycles.append('yamaha')

motorcycles.append('suzuki')

print(motorcycles)

Copy code

The final list is exactly the same as in the previous example:

This way of creating lists is extremely common, because you often have to wait for the program to run before you know what data the user wants to store in the program. To control the user, you can first create an empty list to store the values ​​that the user will enter, and then append each new value provided by the user to the list.

  1. Insert element in the list
    using the methods insert () to add a new element at any position in the list. To do this, you need to specify the index and value of the new element.

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')

print(motorcycles)

Copy code

In this example, the value'ducati' is inserted at the beginning of the list; the method insert() adds space at index 0 and stores the value'ducati' in this place. This operation not only each element in the list are the right
one position:

3.2.3
remove elements from the list,
you often need to remove one or more elements from the list. For example, after a player shoots an alien in the sky, you are likely to delete it from the list of surviving aliens; when the user logs out of their account in the web application you created,
you need to remove the user from Delete from the list of active users. You can delete elements in the list based on position or value.

  1. The
    use of
    a language del
    statement to
    delete the sentence
    deleted
    yuan in addition to
    the element
    prime
    location if you know you want to delete the elements in the list, use the del statement.
    motorcycles = ['honda','yamaha','suzuki']
    print(motorcycles)
    ❶ del motorcycles[0]
    print(motorcycles)
    ❶ The code at ❶ uses del to delete the first element in the list motorcycles-'honda' :
    ['Honda','yamaha','suzuki']
    ['yamaha','suzuki'] Use del to delete list elements at any position, provided that the index is known. The following example demonstrates how to delete the list of the second element - 'YAMAHA':
    Motorcycles = [ 'Honda', 'YAMAHA', 'Suzuki']
    Print (Motorcycles)
    del Motorcycles [. 1]
    Print (Motorcycles)
    below The output shows that the second motorcycle has been deleted from the list:
    ['honda','yamaha','suzuki'

  2. The
    use of
    square
    method
    method pop () delete
    delete
    yuan in addition to
    elements of
    prime
    Sometimes you want to delete an element from the list, and then use its value. For example, you may need to obtain the x and y coordinates of the aliens has just been shot, to show the effect of the explosion in the appropriate position; you may in the Web application
    you want to remove the user from the list of active members, and Add to the list of inactive members.
    The method pop() deletes the element at the end of the list and allows you to continue using it. The term & shells
    pop
    out (pop) from the analogy: like a stack list, and delete end of the list of elements corresponding to pop the top element.
    Here is a motorcycle from the list of motorcycles:
    ❶ motorcycles = ['honda','yamaha','suzuki']
    print(motorcycles)
    ❷ popped_motorcycle = motorcycles.pop()
    ❸ print(motorcycles)
    ❹ print(popped_motorcycle)
    we First define and print the list motorcycles (see ❶). Next, we pop a value from this list and store it in the variable popped_motorcycle (see ❷). Then we print the column
    table to verify that a deleted value (see ❸) from them. Finally, we print the pop-up value to prove that we can still access the deleted value (see ❹).
    The output shows that the value'suzuki' at the end of the list has been deleted and it is now stored in the variable popped_motorcycle:
    ['honda','yamaha','suzuki']
    ['honda','yamaha'] What is the
    suzuki
    method pop() Does it work? Assuming that the motorcycles in the list are stored by the time of purchase, you can use the pop() method to print a message indicating which motorcycle was last purchased:
    motorcycles = ['honda','yamaha','suzuki']
    last_owned = motorcycles.pop()
    print("The last motorcycle I owned was a "+ last_owned.title() + ".") The
    output is a simple sentence indicating which motorcycle was the latest purchase:
    The last motorcycle I owned was a Suzuki.
  3. Playing
    pop
    dequeue
    list
    table
    according to any
    any
    any bit
    position
    is set at
    at
    membered
    element
    prime
    fact, you can use the pop () to delete the list of elements in any position by specifying the element to be deleted in parentheses Index is fine.
    motorcycles = ['honda','yamaha','suzuki']
    ❶ first_owned = motorcycles.pop(0)
    ❷ print('The first motorcycle I owned was a '+ first_owned.title() +'.')
    First, we The first motorcycle in the list pops up (see ❶), and then a message about this motorcycle is printed (see ❷). The output is a simple sentence describing the
    first motorcycle I bought: The first motorcycle I owned was a Honda.
    Don’t forget, whenever you use pop(), the element that was popped up is no longer in the list Up.
    If you are not sure whether to use the del statement or the pop() method, here is a simple criterion: if you want to delete an element from the list and no longer use it in any way, use the del statement; if you want to delete yuan
    after hormone can continue to use it, use pop ().
  4. Root
    According to
    data value
    value to delete
    to delete
    except yuan
    element
    prime
    location in which the value removed from the list of times, you do not know. If you only know the value of the element you want to delete, you can use the remove() method.
    For example, suppose we want to delete the value'ducati' from the list motorcycles. motorcycles = ['honda','yamaha','suzuki','ducati']
    print(motorcycles)
    ❶ motorcycles.remove('ducati')
    print(motorcycles)
    ❶ The code at the place makes Python determine that'ducati' appears in the list Where, and delete the element:
    ['honda','yamaha','suzuki','ducati']
    ['honda','yamaha','suzuki']
    When using remove() to delete an element from the list, You can also use its value later. Below delete the value'ducati' and print a message stating the reason for deleting it from the list:
    ❶ motorcycles = ['honda','yamaha','suzuki','ducati'




    ❹ print("\nA "+ too_expensive.title() +" is too expensive for me.")
    After defining the list at ❶, we store the value'ducati' in the variable too_expensive (see ❷). Next, we use this variable to tell Python which value to remove from the list (see ❸). Finally, the
    value'ducati' has been removed from the list, but it is also stored in the variable too_expensive (see ❹), allowing us to print a message stating the reason for deleting'ducati' from the list motorcycles:
    ['honda', 'yamaha','suzuki','ducati']
    ['honda','yamaha','suzuki']
    A Ducati is too expensive for me.

    Note that the method remove() only deletes the first specified value. If the value to be deleted may appear multiple times in the list, you need to use a loop to determine whether all such values ​​have been deleted.

Guess you like

Origin blog.51cto.com/14972695/2552329