Chapter V list

1, First acquaintance list

1. Understanding of lists

A variable can store an element, while a list is a "big container", which can store n more elements. The program can easily operate these data as a whole. A list is equivalent to an array in other languages, and a list is equivalent to a schoolbag containing many "books"

How lists store data:

The list also has its own id value. The list is a continuous storage space to store the id value of the data to obtain the value value of the data

2. List creation

#First creation
lst_1 = ['hello','world',98]

#The second creation method uses the list function to build
list_2 = list(['hello','world',98])

3. Characteristics of the list

1. The list elements are sorted in order, occupying a continuous storage space
 2. The only data of index mapping is that the index of each position has a specific sequence number, such as list[0]. The element mapped to position 0 can only be 'hello'. Whether it is a positive index or an inverse index (list[-3]), it still gets' hello '
3. Lists can store duplicate data
 4. Any data type can be mixed
 5. The list will dynamically allocate and reclaim memory as needed, without worrying about insufficient or excessive memory

4. List generation

That is, the formula for generating the list. The syntax format is:

[list element expression for custom variable in range (1,10)]
lst = [i*2 for i in range(1,11)]
print(lst)

 

II. List query

Query operation index()

Only one element can be retrieved in turn

lst = ['hello','world',98,'hello']
#If there are the same elements in the list, only the return value of the first element of the same element in the list will be returned. Once indexed, it will break
print(lst.index('hello'))
#If the target of the index is not in the list, an error will be reported

'''You can also search between specified ranges and set the start index position and end index position'''
print(lst.index(98,1,3))    #Retrieve 98 in sequence [1,3]

3, Get list elements

1. Gets a single element in the list

Note that the forward index is from [0, n-1], and the reverse index is from [- N,-1)

print(lst[3])   #Forward index from 0 to n-1
print(lst[-4])  #Reverse index from - N~-1

hello
hello

2. Get multiple elements in the list ---- slice operation

Slice syntax format:

The step size of the list name [start: stop: step] is 1 by default and can be omitted. stop:] also means that the step size is 1

Result of slicing: copy the original list fragment to get a new list

Range of slices: [start,stop)

When step is positive, start slicing from front to back:

[: stop:step] -------------- > the first element of the slice is the first element of the list by default

[start::step] -------------- > the last element of the slice is the last element of the list by default

When step is negative, slice from back to front:

[: stop:step] -------------- > the first element of the slice is the last element of the list by default

[start::step] -------------- > the last element of the slice is the first element of the list by default

lst_2 = [10,20,30,40,50,60,70,80]
print('Original list',id(lst_2))
lst_3 = lst_2[1 : 6 : 2]
print('Sliced list',id(lst_3))      #The change of the list id value indicates that a new list object has been generated

Original list 1585279841728
Sliced list 1585276221440

When step is negative

print(lst_2[::-1])    #When step is negative, LST is output in reverse order_ 2[7::-1]

[80, 70, 60, 50, 40, 30, 20, 10]

When start is a negative number, it means to take the last few values, because when it is a negative number, it means that the reverse value is taken from the last number (the index is - 1), and step is a positive number, so the positive order is obtained

lst = [10,20,30,40,50,60,70]
lst2 = lst[-4:]
print(lst2)

[40, 50, 60, 70] output the last three elements

When stop is negative, the reverse value is taken, but start defaults to 0, that is, the value from the index of 0 to the reverse index of - 2

lst = [10,20,30,40,50,60,70]
lst2 = lst[:-2]
print(lst2)

[10, 20, 30, 40, 50] from 0 to - 2

Continuous slice operation

lst = [10,20,30,40,50,60]
lst_1 = lst[:4][:2]
print(lst_1)

Equivalent to lst[:4]=[10,20,30,40]

                           lst[:4][:2]=[10,20]

The three parameters of the slicing operation can be expressed

lst = [10,20,30,40,50,60]
lst_1 = lst[1*1:3+3][0:5%2]
print(lst_1)

In theory, you can slice continuously for an infinite number of times, as long as the last returned object is still a non empty cutable object

Slice operations for other objects

(1) Slice of tuples

a = (1,2,3,4,5,6)[:3]    #Tuple slicing operation
print(a)

(1, 2, 3) take the first three

(2) Slicing operation of string

b = 'ASBJFS'[:3]
print(b)

ASB # takes the first three characters

(3) Slice application

Use the range function to generate an integer of 1-20, take a multiple of 3 and output the last four bits

for i in range(1,20)[2::3][-4:]:
    print(i,end='  ')

 9  12  15  18 

3. Determines whether the specified element exists in the list

Syntax format:

Element {in/not in list name

lst_4 = [10,20,30,40]
print(10 in lst_4)        #True
print(20 not in lst_4)    #False

4. Traversal of list elements

lst_5 = [10,20,30]
for item in lst_5:    #From lst_ In 5, the values are assigned to item and printed out
    print(item)

10
20
30

4, Add element to list

How to add elements to the list:

1. append(): adds an element to the end of the list

lst = [10,20,30]
lst.append(100)
print(lst)         #[10, 20, 30, 100], the id value does not change after increasing

2. extend(): add at least one element to the end of the list

lst = [10,20,30]
lst2 = [60,70,80]
lst.append(lst2)      #Add an element and insert lst2 as a whole at the end
print(lst)
lst.extend(lst2)
print(lst)              #Insert all elements of lst2 into LST in turn

[10, 20, 30, [60, 70, 80]]
[10, 20, 30, [60, 70, 80], 60, 70, 80]

You can see that append() directly inserts the whole list of lst2 into LST as an element, while extend() inserts all the elements in lst2 into LST in turn.

3. insert(): add an element anywhere in the list

lst.insert(2,100)       #Insert 100 at index 2
print(lst)

4. Slice: add at least one element anywhere in the list

lst3 = ['ds',22,80]
lst[1:] = lst3          
#From the position with index 1, stop is not set to default to the last element, so it is cut from index 1 and lst3 is inserted
print(lst)

 [10, 'ds', 22, 80]

If you insert multiple elements in one index position, you only need to set lst = [2:2], and the start index and end index are in the same position

lst3 = ['ds',22,80]
lst[1:1] = lst3          #Set the start index as the end index, and insert a new element at the index position
print(lst)

[10, 'ds', 22, 80, 20, 30]

5, Delete list

1. remove(): delete one element at a time. Only the first element will be deleted for duplicate elements. If the element does not exist, an exception will be thrown

lst = [10,20,30,40,50,30]
lst.remove(30)         The first 30 is deleted
print(lst)

[10, 20, 40, 50, 30] 

2. pop(): deletes an element at a specified index position

lst.pop(1)             The index is removed as one
print(lst)
lst.pop()              The last element is deleted by default
print(lst)

 [10, 40, 50, 30]
[10, 40, 50]

3. Slice: delete at least one element at a time

lst2 = [10,20,30,40,50]
new_lst = lst2[1:3]
print(new_lst)             Deleting at least one element will result in a new list object

Instead of generating new list objects, delete the contents of the original list

lst2[1:3] = []
print(lst)

 [10, 40, 50]

4. clear(): clear the list

lst2.clear()    Get an empty list
print(lst2)

5. del: delete list

del  lst2

Vi. list modification

Modification of list elements

1. Assigns a new value to the element of the specified index

#Modify one value at a time
lst = [10,20,30,40,50]
lst[2] = 200
print(lst)

2. Assigns a new value to the specified slice

#Slice replacement, index replaced from 1-2 to [100200300400]
lst[1:3] = [100,200,300,400]
print(lst)

7, Summary

1. List query method

        index(data,start,stop)

2. Method of adding elements to the list

append(data): adds an element to the end of the list

extend(data): add at least one element to the end of the list

Insert (index position, data): add an element anywhere in the list

Slicing: adding at least one element anywhere in the list is equivalent to replacing

3. Method of deleting list elements

remove(data): delete one element at a time. Only the first element is deleted for duplicate elements. No exception is thrown

Pop (index position): deletes the element at the specified index position without specifying the default last one

Slice: delete at least one element at a time. If you don't want to generate a new sequence, replace it with an empty list

clear(): clear the list

del: delete list

8, Multifunctional slice

Slicing can implement the method of adding elements to the list

lst3 = ['ds',22,80]  take lst Replace all with from index 1 in lst3
lst[1:] = lst3         
print(lst)

If you set the stop value, you can modify the list, which is equivalent to replacement

lst[1:3] = [100,200,300,400]
print(lst)

You can also delete list elements, but a new list will be generated

lst2 = [10,20,30,40,50]
new_lst = lst2[1:3]
print(new_lst)

If you don't want to generate a new list and still operate in the original list, you just need to replace it with an empty list

lst2[1:3] = []
print(lst)

Keywords: Python network Network Protocol p2p

Added by nthomp on Sat, 05 Mar 2022 00:24:30 +0200