List of articles

    • 1. Get the specified element
    • a) Slicing operation
    • b) Query operation
    • c) List element traversal
    • a) Gets the index of the specified element in the class table
    • b) Get a single element in the list
    • 2. List query operation
    • 3. Addition of list elements
    • 4. Deletion of list elements
    • 5. Modification of list elements
    • 6. Sorting operation of list
    • 7. List generator
    • 1. Why use lists ?
    • 2. The memory diagram of the list explains
    • 3. The creation of list objects
    • One . The concept of list
    • Two . Use of lists
    • summary


One . The concept of list

1. Why use lists ?

We all know that variables can store an element , And the list is a big container , Can be stored N Elements , The program can operate these elements conveniently .

Lists are equivalent to arrays in other languages , If you don't know other languages , This sentence can be omitted .

stay Python Everything is an object , So the variable stores a reference to an object .

2. The memory diagram of the list explains

List diagram :
 Insert picture description here

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38a=20
lst=['hello','world',2021]print(id(lst))print(type(lst))print(lst)

 Insert picture description here

Sketch Map :
Suppose a list is lst=[hello,world,100,20] 4 Elements
 Insert picture description here

3. The creation of list objects

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38#c The first way to create a list lst=['hello','world']# The second way to create a list , Use built-in functions list()lis2=list(['hello','world'])

Features of lists :

  • List elements are sorted in order
  • Index maps the only data
  • Lists can store data repeatedly
  • Mixed storage of any data type
  • Dynamically allocate and reclaim memory as needed

Two . Use of lists

1. Get the specified element

a) Gets the index of the specified element in the class table

Gets the specified element according to the index

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=['hello','world',38,'itlaoxin']print(lst.index('hello'))print(lst.index(38,1,4))

The index is 1-4 Look for elements in 38, The result is 2

b) Get a single element in the list

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=['hello','world',38,'itlaoxin','hello','world',234]print(lst[2]) #38print(lst[-3])#helloprint(lst[0]) #hello#print(lst[10]) # Out of range error print(lst[-1]) #234

2. List query operation

a) Slicing operation

When the step size is positive :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,5,6,7,8,9,]print(lst[1:6:1]) #[2, 3, 4, 5, 6]

lst[1:6:1] Express : From the index 1 Start to index 6 end , In steps of 1, Generally, the default step size is 1

Other writing :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,5,6,7,8,9,]print(lst[1:6:1]) #[2, 3, 4, 5, 6]print(lst[1:6])print(lst[1:6:])print(lst[1:6:2])print(lst[:6:2])

result :
 Insert picture description here
When the step size is negative

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,5,6,7,8,9,]print(lst[::-1])

This kind of situation is equivalent to reverse

This kind of writing can also :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,5,6,7,8,9,]print(lst[8::-1]) #[9, 8, 7, 6, 5, 4, 3, 2, 1]

b) Query operation

Determine whether the specified element exists in the list

  • Elements in List name
  • Elements not in List name
c) List element traversal

for Iteratable variable in list

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,5,6,7,8,9,]for item in lst:
    print(item)

3. Addition of list elements

Addition of list elements :
append() Add an element to the end of the list
extend() Add an element at the end of the list
insert() Add an element anywhere in the list
section : Add an element anywhere in the list

append()

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3]print(" Before adding elements ",lst,id(lst))lst.append(100)print(' After adding elements ',lst,id(lst))

 Insert picture description here
You can see ,ID It's the same , That is, no new list is generated

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3]print(" Before adding elements ",lst,id(lst))lst.append(100)print(' After adding elements ',lst,id(lst))lst2=['hello','world']lst.append(lst2)print(lst)

Output :
 Insert picture description here
You can see here , take lst2 Add to the end as an element
extend()
This is an extension

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3]lst2=['hello','world']print(lst)lst.extend(lst2)print(lst)

 Insert picture description here
insert() Is to add... Anywhere

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3]lst.insert(2,90)print(lst)

 Insert picture description here

Slicing can also add elements anywhere

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]lst3=['hello','world','itlaoxin']lst[1:]=lst3
print(lst)

 Insert picture description here
You can see , It puts the index 1 The back is cut off , And then put lst3 Add to lst In the

Slicing is actually cutting off a part , Replace with a new one .

4. Deletion of list elements

  • remove()
    Delete one element at a time , Duplicate elements delete only the first , Element does not exist valueerror

  • pop()
    Delete an element at the specified index position
    Specifies that the lock is not thrown indexError
    No index specified , Delete the last element in the list

  • section : Delete at least one element at a time ( Slicing produces a new list object )

  • clear() clear list

  • del() Delete list

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]lst.pop(2)print(lst) #[1, 2, 4]

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]#pop()  Delete the last... Without specifying an index lst.pop()print(lst)#[1, 2, 3]

clear list

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]#clear()  clear list lst.clear()print(lst)

Delete list

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]del lst
print(lst)

After deleting it, it's gone , Printing will prompt that there is no definition

 Insert picture description here

5. Modification of list elements

Modification of list elements :

  • Assign a new value to the specified index element
  • Assign a new value to the specified slice
#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]# Modify one value at a time lst[2]=100
print(lst)#[1, 2, 100, 4]

Use slices to replace

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4]lst[1:3]=[30,40,50,60]print(lst)

 Insert picture description here
This is just a 2 and 3 Get rid of , And then use 30,40,50,60 Instead of

6. Sorting operation of list

The two methods :

  1. call sort() Method , All the elements in the list are sorted from small to large by default , You can specify reverse=True Sort in descending order
  2. Call built-in functions sorted() , You can specify reverse=True Sort in descending order , The original list doesn't change . This method produces a new list object
#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,88,77,5,46]print(' The list before sorting ',lst)lst.sort()print(' Sorted list ',lst)lst.sort(reverse=True)print(' null ',lst)

 Insert picture description here
Use sorted() A new list will be generated

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[1,2,3,4,88,77,5,46]print(' The list before sorting ',lst)newlst=sorted(lst)print(lst)print(newlst)

The two differences are : sort() Is to operate on the original list ,sorted() Is to generate a new list

7. List generator

List generator , It's the formula of list generation

grammar : ii for i in range(1,10)
 Insert picture description here
ii Expressions that represent list elements

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[ i for i in range(1,10)]print(lst)

 Insert picture description here

Be careful : To use list generation , The list you need to generate has certain rules

For example, the value of an element in a list is 2,4,6,8,10

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38lst=[ i*2 for i in range(1,6)]print(lst)

In this way, you can understand it .

summary

The list is still important , stay Python It is often used in later study .