List foundation of Python

Time:2022-5-2

”’
List: there are all types of data, and different types of data can be stored
Can be accessed by subscript. Subscript starts at 0

”’

List creation

list_1 = [1,2,3,'4']
list_2 = list()

ergodic

for i in list_1:
    print(i, end=' ')
print(" ")

Get list data according to subscript

value = list_1[-1]
#Print ('last element:% s'% (value))

section

list_2 = list_1[:3]
#Print ('1st to 3rd elements:% s'% (list_2))

list_2.clear()
list_2 = list_1[1:3]
#Print ('2nd to 3rd element:% s'% (list_2))

list_2.clear()
list_2 = list_1[::2]
#Print ('based on element with span 2:% s'% (list_2))

list_2.clear()
list_2 = list_1[::-1]
#Print ('List inversion:% s'% (list_2))

Method: add

list_1.clear()
#Print ('before adding elements to the list, the value is:% s'% (list_1))
list_1.append(5)
list_1.append(6)
#Print ('after adding elements with append, the value is:% s'% (list_1))

list_1.clear()
list_2=[7,8,9]
#Print ('before adding elements to the list, the value is:% s'% (list_1))
list_ 1. Extend (list_2) # is equivalent to list_ 1.extend([7,8,9])
#Print ('after adding elements with extend, the value is:% s'% (list_1))

#Slice addition
list_1=[1,2,3]
#Print ('before adding elements to the list, the value is:% s'% (list_1))
list_ 1 [- 1:] = [4,5] # equivalent to list_ 1[len(list_1):] = [4,5]
#Print ('after adding elements using slices, the value is:% s'% (list_1))

list_1=[1,2,3]
#Print ('before inserting elements into the list, the value is:% s'% (list_1))
list_ 1. Insert (1,4) # inserts an element at the specified position
#Print ('after inserting element 4 in the second position, the value is:% s'% (list_1))

Method: delete

list_1=[1,2,3,4,5,6,4,6,3,4]
#Print ('before deleting elements from the list, the value is:% s'% (list_1))
list_ 1. Remove (4) # deletes the first element. If the deleted element does not exist, an error will be reported
#Print ('after deleting element 4 with remove, the value is:% s'% (list_1))

list_1=[4,1,2,3]
#Print ('before deleting elements from the list, the value is:% s'% (list_1))
list_ 1. Pop (0) # deletes the element at the specified position. 0 indicates the subscript of the element to be deleted
#Print ('after deleting the first element with pop, the value is:% s'% (list_1))

list_1=[1,2,3]
#Print ('before emptying the list element, the value is:% s'% (list_1))
list_ 1. Clear() # clear list elements
#Print ('after emptying the list element, the value is:% s'% (list_1))

replace

list_1= [1,2,3,4]
#Print ('before replacing the list element, the value is:% s'% (list_1))
list_1[2:] = [2,4,4,4]
#Print ('after replacing all elements after the third element in the list, the value is:% s'% (list_1))

sort

list_1= [4,2,1,8,4]
#Print ('List element before sorting, value:% s'% (list_1))
list_1.sort()
#Print ('use sort() to sort the list elements, and the value is:% s'% (list_1))

reversal

list_1= [1,2,3,4]
#Print ('List element before inversion, value:% s'% (list_1))
list_ 1. Reverse() # is equivalent to list_ 1[::-1] 
#Print ('inverted list element, value:% s'% (list_1))

Find the number of occurrences of the element

list_1= [1,2,3,4,3,4,5,3]
#Print ('List element, value:% s'% (list_1))
#Print ('number of occurrences of list element 4, value:% d '% (list_1. Count (4)))

Find the index value of the element

list_1= [1,2,3,4,3,4,5,3]
#Print ('List element, value:% s'% (list_1))
#Print ('index of the first occurrence of list element 4, value:% d '% (list_1. Index (4)))

List operator (+: two lists are spliced; * n: list elements are repeated N times)

s=[1,2]
t=[3,4]
#Print ('List s element, value:% s'% (s))
#Print ('List t element, value:% s'% (T))
#Print ('value after splicing with S + T is:% s'% (s + T))
#Print ('use s * 3 to repeat the elements in the list s for 3 times, and the value is:% s'% (s * 3))

Copy (shallow copy) = = > not a copy reference

list_1 = [1,2,3,4]
#Print ('list_1 element, value:% s'% (list_1))
list_ 1_ copy = list_ 1. Copy() # is equivalent to list_ 1_ copy1 = list_ 1[:]
#Print ('List element after shallow copy, value:% s'% (list_1_copy))

Copy (deep copy) = = > copy reference, reallocate a memory space

import copy
x=[[1,2,3],[4,5,6],[7,8,9]]
#Print ('List x element, value:% s'% (x))
y = copy.deepcopy(x) 
#Print ('List y element after deep copy, value:% s'% (y))
x[1][1] = 4
#Print ('after modifying the source list x [1] [1] element, the value is:% s'% (x)) # comparison between the source list after deep copy and the list after copy
#Print ('List y element, value:% s'% (y))

2D list creation

A=[0]*3
for i in range(3):
    A[i] = [0]*3
#Print ('2d list created with normal for loop:% s'% (a))
A = [[0]*3 for i in range(3)]
#Print ('2d list created using list derivation:% s'% (a))
A[1][1] = 2
#Print ('for the two-dimensional list created by list derivation, after modifying the element value of a [1] [1], the list:% s'% (a))

List derivation

x = [1,2,3,4,5]
#Print ('source list:% s'% (x))
x = [i*2 for i in x]
#Print ('after multiplying all elements in the source list by 2:% s'% (x))

X = [i for i in range(0,10) if i%2==0]
#Print ('use list derivation to get even list within 10:% s'% (x))

Recommended Today

[NOIP2001 Popularization Group] Seek Ranking

Question analysis: The question mentions the pre-order, in-order, and post-order arrangement of trees, so we need to know what these three arrangements mean first. 3 (depth-first) permutations of binary trees: Pre-order, "root left and right". That is, for each subtree of the binary tree, first visit its root, and then traverse its left and right […]