python learning 5-list creation, addition, deletion, modification, and sorting

Insert image description here

1. How to create a list

1. Use square brackets
lst=['hello','world',98]
2. Call the built-in function list()
lst2=list(["hello",'world',98])

2. Characteristics of lists

  • 1. Sort list elements in order
  • 2. Index mapping unique data
  • 3. Lists can store duplicate data
  • 4. Mixed storage of any data types
  • 5. Dynamically allocate and reclaim memory as needed

3. List query operation

1. Get the index of the specified element in the list index()
#如果查找列表中有N个相同元素,只返回相同元素中的第一个元素的索引
lst=['hello','world',98,'hello']
print(lst.index('hello'))
#如果查询的元素在列表中不存在,则会抛出ValueError
print(lst.index('python'))
#还可以在指定的start和stop之间进行查找
print(lst.index('hello',1,3))  #ValueError: 'hello' is not in list   "world",98
print(lst.index("hello",1,4))   #"world",98,"hello"
2. Get a single element in the list
#正向索引从0~N-1  逆向索引从-N到-1 指定索引不存在,抛出IndexError
lst2=['hello','world',98,'hello','world',234]
#获取索引为2的元素
print(lst[2])
print(lst2[-4])
#获取索引为10的元素
print(lst2[10])  #IndexError: list index out of range
3. Get multiple elements in the list_slicing operation syntax format list name [start:stop:step]
  • step is a positive number
lst=[10,20,30,40,50,60]
print("原列表:",id(lst)) #1713813270848
lst2=lst[1:6:1]
print("切的片段:",id(lst2)) #1713808072640  切出来的是新的列表对象
print(lst[1:6])  #[20, 30, 40, 50, 60] 默认步长为1
print(lst[1:6:]) #也代表步长为1
  • step is a negative number
    The first element of the slice is the last element of the list by default
print(lst[::-1])  #[60, 50, 40, 30, 20, 10]
print(lst[6:0:-1]) #[60, 50, 40, 30, 20] 需要注意:stop的项是到此结束,但是不包括此项
4. Determine whether the specified element exists in/not in the list
print(10 in lst) #True
print("y" in lst) #False
5. Traversal of list elements for…in
for item in lst:
    print(item)

4. Adding list elements

1. append() adds an element to the end of the list
print("添加之前:",lst)  #[10, 20, 30, 40, 50, 60]
print(id(lst))
lst.append(100)
print("添加之后:",lst)  #[10, 20, 30, 40, 50, 60, 100]
print(id(lst))   #id相同,说明是同一个列表对象
2. extend() adds at least one element to the end of the list
lst2=['hello','world']
lst.append(lst2)   #不能将它直接print,它必须单独的写 [10, 20, 30, 40, 50, 60, 100, ['hello', 'world']]
lst.extend(lst2)   #[10, 20, 30, 40, 50, 60, 100, 'hello', 'world']
3. insert() adds an element at any position in the list
lst.insert(1,90)  #在索引为1的位置上插入90这个元素 [10, 90, 20, 30, 40, 50, 60, 100, 'hello', 'world']
4. Slicing: Add at least one element anywhere in the list. The essence is replacement.
lst3=[True,False,'python']
lst[1:]=lst3 #[10, True, False, 'python']  从索引1开始之后的所有元素用新的列表元素替换

5. Deletion operations of list elements (the first four are to delete elements, and the last del is to delete the list)

1. remove() deletes one element at a time. If there are duplicate elements, only the first one will be deleted. If the element does not exist, a ValueError will be thrown.
lst4=[10,20,30,40,50,20]
lst4.remove(10)   #[20, 30, 40, 50]
lst4.remove(100) #ValueError: list.remove(x): x not in list
lst4.remove(20) #[10, 30, 40, 50, 20]
2. pop() deletes an element at a specified index position. If the specified index does not exist, an IndexError will be thrown. If the index is not specified, the last element in the list will be deleted.
lst4.pop(1) #[10, 40, 50, 20]
lst4.pop(10) #IndexError: pop index out of range
lst4.pop() #[10, 30, 40, 50]
3. Slicing: Deleting at least one element at a time will produce a new list object.
new_list=lst4[1:3]
print("切片前的列表:",lst4) #[10, 30, 40, 50]
print("切片后的列表:",new_list)  #[30, 40]
#不产生新的列表对象,而是删除原列表中的内容 本质是替换
lst4[1:3]=[]   #[10, 50] 把从索引1-2用空元素代替,相当于将中间部分切除
4. clear() clears all elements of the list
lst4.clear() #[]
5. del deletes the list and deletes the list object
del lst4  #NameError: name 'lst4' is not defined

6. Modification operations of list elements

1. Assign a new value to the element at the specified index
lst=[10,20,30,40]
lst[2]=100  #[10, 20, 100, 40]
2. Assign a new value to the specified slice and modify the multi-value
lst[1:3]=[100,200,300,400]   #[10, 100, 200, 300, 400, 40]

7. List sorting operations (two types, the sort() method changes the original list, and the sorted() method generates a new list object)

1. Using the sort() method, all elements in the list are sorted from small to large by default. You can specify reverse=True to sort in descending order.
lst=[20,40,10,98,34]
print("排序前的列表:",lst)   #[20, 40, 10, 98, 34]
lst.sort()
print("排序后的列表:",lst)   #[10, 20, 34, 40, 98]  id相同,是在原列表的基础上进行的
lst.sort(reverse=True)   #reverse=True表示降序排序,reverse=False表示升序排序,默认是升序排序
print(lst) #[98, 40, 34, 20, 10]
2. Call the built-in function sorted(), you can specify reverse=True to sort in descending order, and the original list will not change (a new list object will be generated)
new_list=sorted(lst)
print(new_list)  #[10, 20, 34, 40, 98]
desc_list=sorted(lst,reverse=True)
print(desc_list) #[98, 40, 34, 20, 10]

8. List generation

lst=[i for i in range(1,10)]
print(lst)  #[1, 2, 3, 4, 5, 6, 7, 8, 9]
lst=[i*i for i in range(1,10)]  #表示列表元素的表达式中通常包含自定义变量,表示列表元素的表达式:i*i
print(lst)  #[1, 4, 9, 16, 25, 36, 49, 64, 81]
lst2=[i*2 for i in range(1,6)]
print(lst2) #[2, 4, 6, 8, 10]

Guess you like

Origin blog.csdn.net/qq_43757976/article/details/130343837