程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

4.9 Python list

編輯:Python

4.9 list

Python Data type No 1 Kind of : character string (str), What is enclosed in quotation marks .
Python Data type No 2 Kind of : Integers (int).
Python Data type No 3 Kind of : Floating point numbers (float), A number with a decimal point .
Python Data type No 4 Kind of : list (list), In square brackets [ ] Express .

4.9.1 The concept of list

A variable can store an element , A list can be understood as a container larger than a variable , You can store multiple elements .
A list is a data structure used to store a set of ordered data elements .
List with [ ] Express , The elements are separated by commas .
Lists are variable data types . Variable means that it can be increased 、 Delete 、 Change element .
Be careful :list As Python Keywords and built-in functions , Variable names are not recommended to be named list.
The list syntax is shown in the following figure :

4.9.2 Create a new list

Create a new empty list

When there is no element in square brackets , A list is an empty list .

# Create a new empty list 
num_list = [ ]

Create a new one int list

When all the elements in square brackets are int Type , This list is int Type list .

# Create a new one int list 
int_list = [1, 2, 3, 4]
# view list 
int_list
[1, 2, 3, 4]

int_list Is the variable name I give the list ;
The list is in square brackets :[ ]
1 2 3 4 Is the element of the list , The above list includes 4 Elements ;
Use English commas between elements , Separate ;
Punctuation must be entered in English , Errors will be reported in Chinese .

Create a new one str list

When all the elements in square brackets are str Type , This list is str Type list .

# Create a new one str list 
str_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ']
# view list 
str_list
[' Zhang San ', ' Li Si ', ' Wang Wu ']

‘ Zhang San ’, ‘ Li Si ’ ,' Wang Wu ’ Is the element of the list , The element type is string , The string must be quoted .

Create a new one int+str list

When the element in square brackets has int type , And then there is str type , This list is int+str type .

# Create a new one int+str list 
int_str_list = [' Zhang San ', 3000 ,' Li Si ', 4000 ,' Wang Wu ', 5000]
# see int+str list 
int_str_list
[' Zhang San ', 3000, ' Li Si ', 4000, ' Wang Wu ', 5000]

4.9.3 Copy of list

Copying lists is the same as copying strings , Also used * The operator .
Copying a list refers to copying the elements of a list , A list is also a list , Only the elements have been added .

# Create a new one int list 
int_list = [1, 2, 3, 4]
# Copy list 
int_list*3
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

int_list The list had 4 Elements , Copy 3 List after times 12 Elements .

4.9.4 List merge

List merging is to merge the contents of existing list elements together , There are two ways :

Method 1 : utilize + The operator ;

# newly build 3 A list 
list_1 = [ ' Zhang San ' , 3000 ]
list_2 = [ ' Li Si ' , 4000 ]
list_3 = [ ' Wang Wu ' , 5000 ]
# List merge 
list_1+list_2+list_3
[' Zhang San ', 3000, ' Li Si ', 4000, ' Wang Wu ', 5000]

Method 2 : utilize extend( ) function .

extend[ɪkˈstend]: Expand .
grammar :A.extend(B) Will list B Merge to list A in .

# newly build 3 A list 
list_1 = [ ' Zhang San ' , 3000 ]
list_2 = [ ' Li Si ' , 4000 ]
list_3 = [ ' Wang Wu ' , 5000 ]
# Will list 2 The elements of are merged into the list 1 in 
list_1.extend(list_2)
# View the list after the first merge 
list_1
[' Zhang San ', 3000, ' Li Si ', 4000]
# Will list 3 The elements of are merged into the list 1 in 
list_1.extend(list_3)
# View the list after the second merge 
list_1
[' Zhang San ', 3000, ' Li Si ', 4000, ' Wang Wu ', 5000, ' Wang Wu ', 5000]

4.9.5 Insert a new element into the list

The list is variable , You can add new elements to the list . There are two main methods :

Method 1 : utilize append( ) function

append( ) Function means to insert a new data element at the end of the list .
grammar : list .append( Elements to be added )
append[əˈpend]: additional .

# Create a new list 
name_list = [' Zhang San ',' Li Si ']
# Add an element king five at the end of the list 
name_list.append(' Wang Wu ')
# See the list after adding elements 
name_list
[' Zhang San ', ' Li Si ', ' Wang Wu ']

Method 2 : utilize insert( ) function

insert( ) Function is to insert a new data element at the specified position in the list .
grammar : list .insert( Indexes , Elements to be added )
insert [ɪnˈsɜːt]: Insert .

# Create a new list 
str_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ']
# Add elements after Zhang San 3000
str_list.insert(1,' Bai Jingting ')
# See the new list 
str_list
[' Zhang San ', ' Bai Jingting ', ' Li Si ', ' Wang Wu ']

insert The first parameter in the parentheses of the function is to increase the index of the element ;
Add the element Bai Jingting after Zhang San , Zhang San's index is [0], The index of Bai Jingting in the list is [1], therefore insert The first argument to the function is 1.

We can think of the list as a hotel , Every room in the hotel has a number , Only the room number of this hotel is from 0 At the beginning , Zhang San lives in the first room , The number is 0; Bai Jingting wants to live in the second room , The number of the second room is 1.

4.9.6 Count the number of occurrences of list elements

utilize count Function can count the number of times an element appears in the list .
grammar : list .count( Elements to be counted )
count [kaʊnt]: Count .

for example : Ranking top in the whole school 5 Of course 5 A list of students' corresponding classes , Check out how many people in your class are on this list .

# Create a new list 
class_list = [' Class one ', ' Class three ', ' Class two ',' Class one ', ' Class one ']
# Check the number of times a shift appears 
class_list.count(' Class one ')
3

The result shows that the first class appears in the list 3 Time .

4.9.7 View the index of the list element

grammar : list .index( Elements )
index [ˈɪndeks]: Indexes .
See where the element appears , Is to see where the element is in the list , That is, view the index .
Before the final exam 5 My classmates are in descending order , Take a look at the fifth row of Wang .

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# Check the index of Wang Wu 
name_list.index(' Wang Wu ')
2

The output is 2, The index of Wang Wu in the list is 2, Then he ranks first in the class 3 name .

4.9.8 View list elements

The way to view list elements is the same as to view character indexes , There are two methods: general index and slice index .

Method 1 : General index

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# View the first element 
name_list[0]
' Zhang San '

Indexes [0] Represents the first place in the list .

Method 2 : Slice indices

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# Look at the first 2 Bit to bit 5 position , Excluding 5 position .
name_list[1:4]
[' Li Si ', ' Wang Wu ', ' Zhao Liu ']

The first 2 The bit index is [1], The first 4 The bit index is [4];
Slice before and after , So this slice is [1:4]

4.9.9 Delete list elements

Grammar 1 : list .pop( Indexes )
Grammar II : list .remove( Elements to delete )
remove [rɪˈmuːv]: Get rid of 、 The abolition of .

Grammar 1 : list .pop( Indexes )

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# Delete Li Si 
name_list.pop(1)
# View the deleted list 
name_list
[' Zhang San ', ' Wang Wu ', ' Zhao Liu ', ' Sun Qi ']

The index of Li Si is [1], So function pop In brackets is 1;
Fallible : It is easy for beginners to pop(1) Write pop[1];
The index uses square brackets when fetching elements [ ] Express ; But the function is followed by English parentheses ( ).

Grammar II : list .remove( Elements to delete )

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# Delete Li Si 
name_list.remove(' Li Si ')
# View the deleted list 
name_list
[' Zhang San ', ' Wang Wu ', ' Zhao Liu ', ' Sun Qi ']

4.9.10 Modify list elements

grammar : list [ Indexes ]= New elements

# New list 
name_list = [' Zhang San ', ' Li Si ' ,' Wang Wu ',' Zhao Liu ',' Sun Qi ']
# Replace Li Si with Bai Jingting 
name_list[1]=' Bai Jingting '
# View the modified list 
name_list
[' Zhang San ', ' Bai Jingting ', ' Wang Wu ', ' Zhao Liu ', ' Sun Qi ']

The index of Li Si is [1], To the right of the equal sign is the element baijingting to be replaced .

4.9.11 Sort the elements in the list

grammar : list .sort( )
sort Sort in ascending order by default .
sort [sɔːt]: classification .

Integer list sort

# Create a new integer list 
number_list = [1,4,9,5]
# Sort list elements 
number_list.sort( )
# View the sorted list 
number_list
[1, 4, 5, 9]

English character sorting

# Create a new string list 
word_list = [ 'andy','food','car','boy']
# Sort list elements 
word_list.sort( )
# View the sorted list 
word_list
['andy', 'boy', 'car', 'food']

4.9.12 summary

Practice after class

Fill in the blank code below .

# newly build 3 A list 
actor_list = [ ' Bai Jingting ',' Zhang jie ',' He Jiong ']
occupation_list = [' Male leading role ',' singer ',' friend ',' host ']
other_list = [' start ',' Happy camp ' ]
# Bai Jingting is the hero of the beginning 
# Output he Jiong is the host of happy camp 
# stay actor_list Add the element Jing Boran at the end of the list 
# stay actor_list Add the element "Xie Na" after Zhang Jie in the list 
# Output the final actor_list

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved