📜  Python中的列出方法 |设置 2 (del、remove()、sort()、insert()、pop()、extend()…)

📅  最后修改于: 2022-05-13 01:54:57.922000             🧑  作者: Mango

Python中的列出方法 |设置 2 (del、remove()、sort()、insert()、pop()、extend()…)

下面的集合 1 中提到了一些列表方法

Python中的列出方法 |设置 1 (in, not in, len(), min(), max()…)

本文讨论了更多方法。

1. del[a : b] :- 此方法删除参数中提到的从索引 'a' 到 'b' 范围内的所有元素

2. pop() :- 此方法删除其参数中提到的位置的元素。

# Python code to demonstrate the working of
# del and pop()
  
# initializing list 
lis = [2, 1, 3, 5, 4, 3, 8]
  
# using del to delete elements from pos. 2 to 5
# deletes 3,5,4
del lis[2 : 5]
  
# displaying list after deleting 
print ("List elements after deleting are : ",end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using pop() to delete element at pos 2
# deletes 3
lis.pop(2)
  
# displaying list after popping  
print ("List elements after popping are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

输出:

List elements after deleting are : 2 1 3 8 
List elements after popping are : 2 1 8 

3. insert(a, x) :- 该函数在其参数中提到的位置插入一个元素。它需要 2 个参数,位置和元素添加到各自的位置。

4. remove() :- 此函数用于删除其参数中提到的第一次出现的数字。

# Python code to demonstrate the working of
# insert() and remove()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using insert() to insert 4 at 3rd pos
lis.insert(3, 4)
  
# displaying list after inserting
print("List elements after inserting 4 are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using remove() to remove first occurrence of 3
# removes 3 at pos 2
lis.remove(3)
  
# displaying list after removing 
print ("List elements after removing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

输出:

List elements after inserting 4 are : 2 1 3 4 5 3 8 
List elements after removing are : 2 1 4 5 3 8 

5. sort() :- 此函数按升序对列表进行排序

6. reverse() :- 这个函数反转列表的元素。

# Python code to demonstrate the working of
# sort() and reverse()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using sort() to sort the list
lis.sort()
  
# displaying list after sorting
print ("List elements after sorting are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using reverse() to reverse the list
lis.reverse()
  
# displaying list after reversing
print ("List elements after reversing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

输出:

List elements after sorting are : 1 2 3 3 5 8 
List elements after reversing are : 8 5 3 3 2 1 

7. extend(b) :- 此函数用于使用另一个列表中存在的元素来扩展列表。这个函数接受另一个列表作为它的参数

8. clear() :- 该函数用于清除列表中的所有元素。此操作后,列表变为空。

# Python code to demonstrate the working of
# extend() and clear()
  
# initializing list 1
lis1 = [2, 1, 3, 5]
  
# initializing list 1
lis2 = [6, 4, 3]
  
# using extend() to add elements of lis2 in lis1
lis1.extend(lis2)
  
# displaying list after sorting
print ("List elements after extending are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")
      
print ("\r")
  
# using clear() to delete all lis1 contents
lis1.clear()
  
# displaying list after clearing
print ("List elements after clearing are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")

输出:

List elements after extending are : 2 1 3 5 6 4 3 
List elements after clearing are : 

相关文章:
列出Python中的方法
Python中的列出方法 |设置 1 (in, not in, len(), min(), max()…)