How to use ython list (detailed version)

1. Sequence

Basic concepts : the most basic one in Pythondata structure. Sequence is used to save a groupOrdered dataAll data has a unique position (index, index) data in the sequence and order of addition will be allocated according to an index in the sequence which
data structure :Data structure is the way a computer stores and organizes data.

Sequence classification

Variable sequence (elements in the sequence can be changed) : such as list (list), dictionary (dict)...
Immutable sequence (elements in the sequence cannot be changed) : such as string (str), tuple (tuple)...

2. List

Basic concept : a finite sequence of data items, that is, a collection of data items arranged in a certain linear order. The basic operations on this data structure include searching, inserting, and Delete
effect
     •List is an object in python language
     • The list can store multiple ordered data
     • The list is an object used to store objects

2.1 Use of lists

Creation of the list :

student=[]#创建一个新列表,表名为student

List can save any object

list = [1 , 'python',None,True,max(1,2,3)]#可以保存整型变量,字符串,空值,布尔值,函数...

The index of the list (index):
       • Get the
       elements in the list by index (index) • Index is the position of the element in the list, each element has its own index, the index starts from 0, the first position of the list It is 0, the second position is 1, and so on.

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊']
print(name[0])#输出结果:喜羊羊
print(name[2])#输出结果:沸羊羊
print(name[4])#输出结果:懒羊羊

       • The index can also be a negative number. If the index is negative, the element is retrieved from back to front. -1 means the last element, -2 means the second to last element, and so on.

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊']
print(name[-1])#输出结果:懒羊羊
print(name[-3])#输出结果:沸羊羊
print(name[-5])#输出结果:喜羊羊

Slicing of the list
       •Slicing refers to obtaining a sublist from an existing list.
       • The specified element can be obtained by slicing.
       • Syntax performance : list [start index: end index: step length]
       •When the element is obtained by slicing, the element at the start position will be included, and the element at the end position will not be included.
       •Only a new list will be obtained by slicing, and the original list will not be affected

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(name[0:3])#输出结果:['喜羊羊', '美羊羊', '沸羊羊']
print(name[2:4])#输出结果:['沸羊羊', '慢羊羊']
print(name)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']

       • If the index of the end position is omitted, it will be intercepted from the current position to the end
       . If the start position is omitted, it will be intercepted from the first element to the end position, but does not include the element at the end position.
       • If the start position and the end position are both If omitted, it will intercept the elements from the first position to the last position, that is, get all the elements

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(name[1:])#输出结果:['美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(name[:4])#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊']
print(name[:])#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']

       •The step size can be omitted, the default value is 1
       •The step size can be negative, but cannot be 0

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(name[0:5])#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊']
print(name[0:5:1])#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊']
print(name[0:5:3])#输出结果:['喜羊羊', '慢羊羊']
print(name[::2])#输出结果:['喜羊羊', '沸羊羊', '懒羊羊']
print(name[::-1])#输出结果:['暖羊羊', '懒羊羊', '慢羊羊', '沸羊羊', '美羊羊', '喜羊羊'] 反转序列
print(name[4:1:-1])#输出结果:['懒羊羊', '慢羊羊', '沸羊羊']

Modification of the list
       • Via delKeywordTo delete the element

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
del name[3]#删除索引为3的元素
print(name)#输出结果为:['喜羊羊', '美羊羊', '沸羊羊', '懒羊羊', '暖羊羊']

       • Modify by slicing (that is, to re-assign the content of the slice, but the content of the assignment must be a sequence)

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name[0:3]=['红太狼','灰太狼','蕉太狼',]#在索引为0,1,2的位置依次添加列表中的新元素
print(name)#输出结果:['红太狼', '灰太狼', '蕉太狼', '慢羊羊', '懒羊羊', '暖羊羊']

       • Remove elements by slicing

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name[1:3]=[]#删除索引为1,2的元素(本质是将其赋值为空列表)
print(name)#输出结果:['喜羊羊', '慢羊羊', '懒羊羊', '暖羊羊']

       • When the step size is set,The number of elements in the sequence must be the same as the number of elements in the slice

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name[::2]=['红太狼','灰太狼','蕉太狼',]#从索引为0开始,步长为1,依次替换其中的元素
print(name)#输出结果:['红太狼', '美羊羊', '灰太狼', '慢羊羊', '蕉太狼', '暖羊羊']

3. General operations of variable sequences

+with*(Stitching and repeating)
       •+ can join two lists into one list
       •* can repeat the list a specified number of times(Note: 2 lists cannot be multiplied, only integers can be multiplied)

name1 = ['喜羊羊', '美羊羊', '沸羊羊']
name2 = ['慢羊羊', '懒羊羊', '暖羊羊']#将两个列表拼接在一起
print(name1+name2)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name = ['喜羊羊', '美羊羊', '沸羊羊']#将列表内容重复三次
print(3*name)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '喜羊羊', '美羊羊', '沸羊羊', '喜羊羊', '美羊羊', '沸羊羊']

in和not in
       •In is used to detect whether the specified element is in the list, and the return isBoolean value
       •Not in is used to detect whether the specified element is not in the list, the return isBoolean value

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']#判断喜羊羊是否在列表中
print('喜羊羊' in name)#在,输出结果:True
name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']#判断慢羊羊是否不在在列表中
print('慢羊羊' not in name)#在,输出结果:False

       • max() Get the maximum value in the list
       • min() Get the minimum value in the list

number = [1, 2, 3, 4, 5, 6, 7, 8]
print(max(number))#输出结果:8
print(min(number))#输出结果:1

       • len() Get the number of elements in the list

name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(len(name))#输出结果:6

       • Method : list.index(x, start index, end index)
       •The first parameter is to get the position of the specified element in the list
       •The second parameter indicates the starting position index of the search
       •The third parameter indicates the end position index of the search

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']#在列表的全部元素中查找则返回第一结果的索引值
print(name.index('懒羊羊'))#输出结果:2
name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']#在指定区间查找返回结果的索引值
print(name.index('懒羊羊', 3, 6))#输出结果:5

       •List.count (element name) statisticsDesignated elementNumber of occurrences in the list

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(name.count('懒羊羊'))#输出结果:2

4. List method

       •list.append() Add an element to the end of the list

name = ['喜羊羊', '美羊羊', '沸羊羊']
name.append('灰太狼')#向列表最后添加'灰太狼'这个元素
print(name)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '灰太狼']

       •list.insert(arg1,arg2) Insert an element into the specified position of the list Parameter 1: The position to be inserted Parameter 2: The element to be inserted

name = ['喜羊羊', '美羊羊', '沸羊羊']
name.insert(1, '灰太狼')
print(name)输出结果:['喜羊羊', '灰太狼', '美羊羊', '沸羊羊']

       •list.extend(iterable) Use a new list to expand the current list (it will add the middle element of the list to the original list) The parameter is a list

name1 = ['喜羊羊', '美羊羊', '沸羊羊']
name2 = ['红太狼', '灰太狼', '蕉太狼']
name1.extend(name2)#向name1列表最后添加neme2列表
print(name1)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '红太狼', '灰太狼', '蕉太狼']

       •list.pop() Delete the element according to the index and return the deleted specified element

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
a = name.pop(2)#删除索引为2的元素,并返回赋值给a
print(name)#输出结果:['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
print(a)#输出结果:懒羊羊

       •list.remove() Delete the specified element (if there are multiple elements with the same value, only the first one will be deleted)

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name.remove('懒羊羊')#相同值只会删除第一个
print(name)#输出结果:name = ['喜羊羊', '美羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name.remove('懒羊羊')
print(name)

       •list.reverse() Flip list

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name.reverse()#翻转列表
print(name)#输出结果:['暖羊羊', '懒羊羊', '慢羊羊', '沸羊羊', '懒羊羊', '美羊羊', '喜羊羊']

       •list.sort(key=None,reverse=False) Used to sort the elements in the list reverse: True reverse order; False positive order

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name.sort(reverse=True)
print(name)#输出结果:['美羊羊', '沸羊羊', '暖羊羊', '懒羊羊', '懒羊羊', '慢羊羊', '喜羊羊']
name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
name.sort(reverse=False)
print(name)#输出结果:['喜羊羊', '慢羊羊', '懒羊羊', '懒羊羊', '暖羊羊', '沸羊羊', '美羊羊']

5. Traverse the list

5.1 for loop to traverse the list

       •Grammatical expression
             •For variable in sequence (traversal rules):
                   code block
       •Note: The code block of the for loop will be executed multiple times, and several elements in the sequence will be executed several times. Each execution will assign an element in the sequence to a variable, so we can get each element in the list through the variable

name = ['喜羊羊', '美羊羊', '懒羊羊', '沸羊羊', '慢羊羊', '懒羊羊', '暖羊羊']
for i in name:
    print(i)
#输出结果:
#喜羊羊
#美羊羊
#懒羊羊
#沸羊羊
#慢羊羊
#懒羊羊
#暖羊羊

5.2 range(start, stop, step)

       • Parameter description
       •start: counting starts from start. The default is to start from 0. For example, range(5) is equivalent to range(0, 5);
       •==stop: count to the end of stop, but stop is not included. For example: range(0, 5) is [0, 1, 2, 3, 4] without 5 ==
       •step: step length, the default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

for i in range(0,5):
    print(i)
#输出结果一样:0 1 2 3 4 
for i in range(5):
    print(i)
for i in range(0,5):
    print(i)
#输出结果一样:0 1 2 3 4
for i in range(0,5,1):
    print(i)

Guess you like

Origin blog.csdn.net/qq_45261963/article/details/107329911