Hướng dẫn how does del work in python list? - del hoạt động như thế nào trong danh sách python?

Bởi vì

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2 chỉ đơn giản là một bản sao của
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3, bao gồm các đối tượng giống hệt nhau là
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 nhưng một đối tượng khác với
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3. Các yếu tố của chúng giống hệt nhau nhưng họ không.

Hãy tạo danh sách và thực hiện một số kiểm tra

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
6:

a = [1, 2, 3]
print(id(a)) # 97731118088
print(id(a[:])) # 97731213576
print(id(a[:])) # 97731212104
print(id(a[:])) # 97731198600

Lưu ý các

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
6 thay đổi của bản sao
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2. Nó là một đối tượng được tạo ra khi bay mỗi khi nó được gọi, và quan trọng nhất là
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
9 không phải
def deletion1(a):
    del a

def deletion2(a):
    del a[:]
0! Chúng ta hãy xem xét các yếu tố của họ để đưa ra kết luận:

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True

Vì vậy, chúng ta có thể kết luận rằng

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2 là một đối tượng bao gồm chính các yếu tố của
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 nhưng là một đối tượng khác với
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 và là một đối tượng khác nhau mỗi khi được gọi. Các yếu tố của
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 và
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2 đều giống hệt nhau, nhưng bản thân chúng thì không.

Vì vậy, những gì

def deletion1(a):
    del a

def deletion2(a):
    del a[:]
7 làm là loại bỏ tất cả các yếu tố của
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3. Theo cách đó
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 bị đột biến và bạn kết thúc với một
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 trống rỗng, tức là
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
1. Tuy nhiên,
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
2 sẽ loại bỏ tên
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 khỏi không gian tên hoàn toàn và khi bạn yêu cầu Python in
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 cho bạn, bạn sẽ nhận được
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
5.elements of
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3. That way
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 is mutated, and you end up with an empty
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3, i.e.
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
1. However,
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
2 removes the name
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 from namespace completely, and when you ask Python to print
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 for you, you'll get a
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
5.

Nhưng làm thế nào để chúng ta biết điều đó? Chà, hãy đạt được một số quan điểm bằng cách tháo rời

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
6 trên
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 so với
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2:
Well, let's gain some perspective by disassembling
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
6 on
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 vs
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
2:

Hãy xác định hai chức năng:

def deletion1(a):
    del a

def deletion2(a):
    del a[:]

Hãy tháo rời họ:

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE

Tài liệu DISC chỉ ra rằng hoạt động

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
9, mà hàm đầu tiên thực hiện, chỉ đơn giản là "xóa cục bộ
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
0". Điều này về cơ bản là loại bỏ tên đó
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 để bạn không thể tiếp cận đối tượng danh sách nữa. Coi chừng, điều này không loại bỏ đối tượng được gọi bằng tên A, nhưng chỉ cần xóa tên của nó để
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 không phải là một tham chiếu đến bất cứ điều gì nữa. Đối tượng
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
3 vẫn là cùng một danh sách,
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
4:this does not remove the object that is referred to by the name a, but just removes its name so that the
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 is not a reference to anything anymore. The object
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
3 is still the same list,
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
4:

import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]

Mặt khác, một lần nữa từ tài liệu,

import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
5 "thực hiện
import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]
6", về cơ bản là "một toán tử tại chỗ sẽ loại bỏ đỉnh của ngăn xếp và đẩy kết quả trở lại trên ngăn xếp". Bằng cách này, các phần tử ngăn xếp được loại bỏ và bạn bị bỏ lại với tên
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3, hiện đề cập đến một danh sách có các yếu tố bị xóa, tức là chỉ là một "vỏ trống", nếu bạn muốn. Sau khi hoạt động này,
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
3 trở thành
import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
1, nhưng vẫn giống nhau
print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
6 của 97731118088. Chỉ là các yếu tố của nó đã được xóa thông qua việc xóa tại chỗ.

Từ khóa Python

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
6 được sử dụng để xóa các đối tượng. Cú pháp của nó là:

# delete obj_name
del obj_name

Ở đây, obj_name có thể là các biến, đối tượng do người dùng xác định, danh sách, mục trong danh sách, từ điển, v.v.


Ví dụ 1: Xóa đối tượng do người dùng xác định


class MyClass:
    a = 10
    def func(self):
        print('Hello')

# Output: 
print(MyClass)

# deleting MyClass
del MyClass

# Error: MyClass is not defined
print(MyClass)	

Trong chương trình, chúng tôi đã xóa MyClass bằng cách sử dụng câu lệnh DEL MyClass.


Ví dụ 2: Xóa biến, danh sách và từ điển


my_var = 5
my_tuple = ('Sam', 25)
my_dict = {'name': 'Sam', 'age': 25}

del my_var
del my_tuple
del my_dict

# Error: my_var is not defined
print(my_var)

# Error: my_tuple is not defined
print(my_tuple)

# Error: my_dict is not defined
print(my_dict)

Ví dụ 3: Xóa các mục, lát cắt khỏi danh sách

Câu lệnh

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
6 có thể được sử dụng để xóa một mục tại một chỉ mục nhất định. Nó cũng có thể được sử dụng để loại bỏ các lát từ một danh sách.

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# deleting the third item
del my_list[2]

# Output: [1, 2, 4, 5, 6, 7, 8, 9]
print(my_list)

# deleting items from 2nd to 4th
del my_list[1:4]

# Output: [1, 6, 7, 8, 9]
print(my_list)

# deleting all elements
del my_list[:]

# Output: []
print(my_list)

Ví dụ 4: Xóa một khóa: cặp giá trị khỏi từ điển

person = { 'name': 'Sam',
  'age': 25,
  'profession': 'Programmer'
}

del person['profession']

# Output: {'name': 'Sam', 'age': 25}
print(person)

del với bộ dữ liệu và dây

Lưu ý: Bạn không thể xóa các mục của bộ dữ liệu và chuỗi trong Python. Đó là bởi vì bộ dữ liệu và chuỗi là bất biến; các đối tượng không thể thay đổi sau khi tạo ra của chúng. You can't delete items of tuples and strings in Python. It's because tuples and strings are immutables; objects that can't be changed after their creation.

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
0

Tuy nhiên, bạn có thể xóa toàn bộ tuple hoặc chuỗi.

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True
1

Del làm gì trong danh sách Python?

Từ khóa DEL được sử dụng để xóa các đối tượng. Trong Python, mọi thứ đều là một đối tượng, vì vậy từ khóa DEL cũng có thể được sử dụng để xóa các biến, danh sách hoặc các phần của danh sách, v.v.delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

Làm thế nào để bạn sử dụng lệnh del trong python?

Làm thế nào để sử dụng del trong Python..
a_varable = 1. del a_varable.Xóa `a_varable` print (a_varable) lỗi.....
a_list = ["a", "b", "c"] del a_list [1] xóa phần tử "b" từ `a_list` print (a_list) đầu ra.....
a_dictionary = {"a": 1, "b": 2} del a_dictionary ["b"] xóa khóa: cặp giá trị "b": 2. in (a_dictionary) đầu ra ..

Sự khác biệt giữa del và pop () trong danh sách là gì?

Hàm pop () được sử dụng để trả về phần tử bị loại bỏ từ chức năng danh sách. Del () được sử dụng để xóa một phần tử tại một số chỉ mục được chỉ định trong danh sách. The del() function is used to delete an element at a specified index number in the list.

Sự khác biệt giữa phương thức danh sách pop () và remove () là gì?

Các phần tử mảng có thể được loại bỏ bằng phương thức pop () hoặc xóa ().Sự khác biệt giữa hai chức năng này là cái trước trả về giá trị bị xóa trong khi cái sau thì không.Hàm pop () không có tham số hoặc giá trị chỉ mục làm tham số của nó.the former returns the deleted value whereas the latter does not. The pop() function takes either no parameter or the index value as its parameter.