Recognize the list and perform various operations on the list

Posted by Skeptical on Sat, 16 Oct 2021 02:49:32 +0200

1. Awareness list

1) The list is the container data type, with [] as the container flag, and the elements inside are separated by commas: [element 1, element 2, element 3,..., element n]

2) Features:

The list is variable. The number of elements, the value of elements, and the order of elements in the list.

The list is ordered - subscript operations are supported.

print([1,2,3] == [3,2,1])
# False

3) The list has no requirements for elements:

Lists can hold any type of data.

The same list can hold different types of data.

2. Get list elements

2.1 subscript acquisition

Writing method: list [subscript]

A list can be a concrete list or a variable that stores the list

Location information of elements in the list when subscribing:

Each element in the list has two sets of subscripts, one set of subscripts increasing from 0 from the front to the back, and the other set of subscripts decreasing from - 1 from the back to the front

list = [10, 20, 30]
print(list[0],[10, 20, 30][0])
print(list[-1],[10, 20, 30][-1])
'''
10 10
30 30
'''

The subscript cannot exceed the limit (not within the list subscript range), otherwise an error will be reported

2.2 slice acquisition: multiple elements are obtained at one time

1) Complete grammar

List [start subscript: end subscript: step size]

Get element procedure:

  1. Whether the element can be obtained: if the start to end direction is the same as the step direction, the element can be obtained; if not, the element cannot be obtained---- Step direction: the step is a positive number, and the direction is from front to back; The step size is negative and the direction is from back to front.
# example:
hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
print(hero_list[2:-1:1]) # Subscripts from 2 to - 1 are from front to back, steps are positive, and directions are the same.
print(hero_list[0:4:-2]) 
print(hero_list[-1:1:-3])
'''
['Irene', 'Fei Zhang', 'Sun Bin']
[]
['Cai Wenji','Irene']
'''

2) Omit step size

List [start subscript: end subscript]

Omitting a step is equivalent to a step of 1, that is, the list [start subscript: end subscript: 1]

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
print(hero_list[1:3])
#['Zhen Ji', 'Irene']

3) Omit start position

List [: end subscript: step size]

Omitting the start position means that there are two cases of the start position. One is that the step size is a positive number and the start position is at the beginning of the list; One is that the step size is negative, and the starting position is at the end of the list

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
print(hero_list[:3:1]) # The step size is a positive number, and the starting position is at the beginning of the list, starting from 'Luban 7'
print(hero_list[:3:-1])	# The step size is negative, and the starting position is at the end of the list -- 'Cai Wenji'
'''
['Luban 7', 'Zhen Ji', 'Irene'] 
['Cai Wenji', 'Sun Bin']
'''

4) Omit end position

List [start subscript: end subscript: step size]

Omitting the end position means that there are two cases of the end position. One is that the step size is a positive number and the end subscript is at the end position of the list; One is that the step length is a negative number, ending the following table at the beginning of the list

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
print(hero_list[3::1]) # The step size is a positive number, and the following table is at the end of the list - after 'Cai Wenji'
print(hero_list[3::-1])# On the contrary, take it in front of Luban 7
'''
['Fei Zhang', 'Sun Bin', 'Cai Wenji']
['Fei Zhang', 'Irene', 'Zhen Ji', 'Luban 7']
'''

5) The start and end subscripts are omitted together

List [:: step size]

If the step size is a positive number, the element is taken from front to back according to the step size; If the step size is negative, the element is taken from back to front according to the absolute value of the step size.

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
print(hero_list[::1])
print(hero_list[::-1])
'''
['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
['Cai Wenji', 'Sun Bin', 'Fei Zhang', 'Irene', 'Zhen Ji', 'Luban 7']
'''

3. Traverse list elements

3.1 get list elements directly with the for loop

Syntax:

for variable in list:

Circulatory body

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
for item in hero_list:
	print(item,end=' ')
# Lu Ban No.7 Zhen Ji, Eileen, Zhang Fei, Sun Bin, Cai Wenji 
3.2 obtain the subscript with a loop, and then obtain the list elements through the subscript

Syntax:

for variable in range:

List [variable]

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
for item in range(len(hero_list)):
	print(hero_list[item],end=' ')
# Lu Ban No.7 Zhen Ji, Eileen, Zhang Fei, Sun Bin, Cai Wenji 

4. Addition, deletion and modification of the list

4.1 add

**1) append - appends the specified element to the list**

List. Append (element)

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
# Add a 'Hou Yi' to the list element
hero_list.append('Hou Yi')
print(hero_list)
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Zhang Fei', 'Sun Bin', 'Cai Wenji', 'Hou Yi']

2) insert - inserts the specified element before the element corresponding to the specified subscript

List. Insert (subscript, element)

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
# Add a 'Hou Yi' before 'Zhang Fei'
hero_list.insert(-3,'Hou Yi')
print(hero_list)
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Hou Yi', 'Zhang Fei', 'Sun Bin', 'Cai Wenji']
4.2 delete

1) del -- delete the element corresponding to the specified subscript. The subscript cannot exceed the boundary

del list [subscript]

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji']
# Delete element with subscript 3
del hero_list[3] # The element with subscript 3 is' Zhang Fei ', and Zhang Fei is deleted
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Sun Bin', 'Cai Wenji']

2) remove - delete the specified element in the list. If the element does not exist, an error will be reported. There are multiple elements, and only the first one will be deleted

List. Remove (element)

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji','Fei Zhang']
hero_list.remove('Nuwa') # ValueError: list.remove(x): x not in list
hero_list.remove('Fei Zhang')
print(hero_list) # Zhang Fei has two. Delete the first one
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Sun Bin', 'Cai Wenji', 'Zhang Fei']

3)pop

List. pop() - take out the last element of the list. The taken element disappears from the list and can be saved by variables

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji','Fei Zhang']
pop_data = hero_list.pop()
print(hero_list,pop_data)
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Zhang Fei', 'Sun Bin', 'Cai Wenji'] Zhang Fei

List. Pop (subscript) - retrieve the elements corresponding to the specified subscript in the list. The extracted elements disappear from the list and can be saved by variables

hero_list = ['Luban 7', 'Zhen Ji', 'Irene', 'Fei Zhang', 'Sun Bin', 'Cai Wenji','Fei Zhang']
pop_data = hero_list.pop(3) # Take out the element with subscript 3, corresponding to -- 'Zhang Fei'
print(hero_list,pop_data)
# ['Luban No. 7', 'Zhen Ji', 'Irene', 'Sun Bin', 'Cai Wenji', 'Zhang Fei'] Zhang Fei
4.3 change - modify the value of the element in the list

List [subscript] = value ----- modify the value of the specified subscript element to the specified value

When modifying list elements, we use the second method of traversing the list, where the elements are located and modified by subscripts

# Exercise: change all unreasonable scores in the scores of the hundred mark system to 0
scores = [90, 180, 30, 67, -238, 89, 2, 34, 1928]
for index in range(len(scores)):
    s = scores[index]
    if s < 0 or s > 100:
        scores[index] = 0
print(scores)       
# [90, 0, 30, 67, 0, 89, 2, 34, 0]

Topics: Python Algorithm data structure