Elements arranged in a specific order !
You can create all the letters of the alphabet 、 Numbers 0~9 Or a list of the names of all family members
Sequence is Python The most basic data structure in !
Each value in the sequence has a corresponding position value , Call it an index , The first index is 0, The second index is 1, And so on and C The array subscript of is similar to , Believe to learn C Language partners can easily grasp this kind of !
use 【】 Express Type of list , Data is separated by comma expressions ! The data item in the list can be anything , This is also prominent Python Can be more flexible to use .
characteristic : Support addition, deletion, modification and query , The data in the list can change , The data in the list can change 【 Data items can vary , The memory address doesn't change 】
list (List) Is an ordered and changeable collection . Allow duplicate members .
Tuples (Tuple) Is an ordered and unchangeable collection . Allow duplicate members .
aggregate (Set) Is an unordered and indexed collection . There are no duplicate members .
The dictionary (Dictionary) It's a disorder , Variable and indexed sets . There are no duplicate members .
When selecting a collection type , It's useful to know the properties of this type .
Choosing the right type for a particular dataset may mean preserving meaning , And may mean improving efficiency or security .
keyword :type()
The following example :
a = []
print(type(a))
Copy code
The output result of the above example
stay Python The list is made up of <[ ]> To express , And use comma expression to split the elements , Next, let's create a list to give you an intuitive feeling :
Mylist = ['apple','orange','banana']
print(Mylist)
Copy code
The output result of the above example
If you want the list to print out, it includes 【】 Bracketed , And use comma expression to split , The following are the list forms that can be created :
Mylist1 = [' Hello ', 'Python', 1997, 2000]
Mylist2 = [1, 2, 3, 4, 5]
Mylist3 = ['%','a','c','D']
Mylist4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(Mylist1)
print(Mylist2)
print(Mylist3)
print(Mylist4)
Copy code
The output result of the above example
Mylist1 The list stored inside is preceded by character type , After that is digital shaping , This illustrates the Python The list can be any type of value , Of course, there can also be Boolean types , Type of floating point number, etc
The list is based on Orderly Set to sort , Order is actually a sort according to a certain order and law .
The index value of the list is the same as that of the string 0 Start , The second index is 1, And so on
The following example : Access the index values in the following code 0,1,2, Note here that the index is from 0 At the beginning , No 1
My_list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(My_list[0])
print(My_list[1])
print(My_list[2])
Copy code
The output result of the above example
Change the value of a specific item , You need to reference the index number to make a change
The following example :
value = ["C","C++",'VB']
value[1] = "Python"
print(value)
Copy code
The output result of the above example
Here we change the value of the second term , It was "C++", We changed it to "Python"
The following example :
My_list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(My_list[0]*2)
print(My_list[1]*2)
print(My_list[2]*2)
print(My_list*2)
Copy code
The output result of the above example
If you want to repeat printing three times, then the same thing , This is common in all lists
Of course, the index can not just start from the front to the back , You can also start from the back to the front .
This is Python Access the list of the last element to provide a special Special grammar , The last index value is :-1, The penultimate index value is :-2
The following example :
My_list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(My_list[-1])
print(My_list[-2])
Copy code
The output result of the above example
Use the subscript index to access the values in the list , You can also use square brackets [ ] Intercepting characters in the form of
The following example :
numbers = [10,20,30,40,100]
print(numbers[0:4])
Copy code
The output result of the above example
If you want to search from the end of the list , Please specify a negative index , Be careful : Here is **-4**( Include ) And then this **-1** Yes. ( exclude ), Attention is to exclude , Then include the value of the first one in the back. Don't forget that it means to remember from the second one
The following example :
types = ['A', 'B', 'C', 'D', 'E','F']
print(types[-4:-1])
Copy code
The above example outputs
The following example :
My_list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(My_list[::-1])
Copy code
The above example outputs
Not all the examples are given here , Will only list the more commonly used
effect :append The function can append elements to the list
give an example :
listA = ['Python',123456,3.14,' Hello ','True']
print(" Before appending :",listA)
listA.append(["123","hello"])
listA.append(666)
print(" After adding :",listA)
Copy code
Running results :
effect :insert Function can insert data at the specified position
give an example :
listA = ['Python',123456,3.14,' Hello ','True']
print(' Before insertion :',listA)
listA.insert(1," This is the data I just inserted ")
print(" After insertion :",listA)
Copy code
Running results :
effect :extend The function returns the specified list element ( Or any iteratable element ) Add to the end of the current list .
give an example :
My_list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(" Before designation :",listA)
E = ['yi','er','san']
listA.extend(E)
print(" After designation :",listA)
cars = list(range(10)) # Cast to list
print(type(cars))
cars.extend(cars*0)
print(cars)
Copy code
Running results :
effect : You can delete elements from the list ( It is also possible to delete multiple elements by cutting )
give an example :
listA = ['Python',123456,3.14,' Hello ','True']
print(' Not deleted :',listA)
del listA[0]
print(' Deleted :',listA) # Note that a layer of elements has been deleted above
del listA[0:2]
print(' Batch deletion :',listA)
Copy code
Running results :
effect : Determine how many items are in the list, that is, its length ,( This is from 1 At the beginning )
give an example :
thislist = ["apple", "banana", "cherry"]
print(" The length is :",len(thislist))
Copy code
Running results :
effect : Method to delete the specified item
give an example :
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Copy code
Running results :
effect : Method to delete the specified index ( If the index is not specified , Then delete the last item )
give an example :
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
thislist.pop(1)
print(thislist)
Copy code
Running results :
effect : Method returns the first occurrence of the specified value , Find the value of the index
give an example :
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
Copy code
Running results :
||🥳
\