Detailed explanation of Python collection pop() function, pop random deletion principle

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

pop() can "randomly delete" an "element" in the collection

grammar

set.pop()

return value

  • return the removed element

Example: Randomly delete an element

set1 = {
    
    'zhangsan', 'lisi', 'wangsu'}

set1.pop()
print(set1)

output:

{
    
    'lisi', 'zhangsan'}

From the pseudo-source code (Python's built-in functions can't see the source code, only the function introduction), we can see that pop() will "delete" and "return" the "elements" in the collection . If the collection is "empty" , it will report an error KeyError.

insert image description here


1. Random deletion is not completely random

pop() is implemented based on HashMap. It always "deletes" the "first element" in the collection . Since the collection is "unordered" , it looks like "randomly" deleting elements.

1.1, pure numbers

When the elements in the collection are "pure numbers" , the collection will arrange the elements in ascending order, and the "smallest" value will not be ranked first, so when pop(), the smallest element will be deleted.

set1 = {
    
    1, 5, 3, 9, 2}

print('删除前:', set1)
set1.pop()
print('删除后:', set1)

First execution output:

删除前: {
    
    1, 2, 3, 5, 9}
删除后: {
    
    2, 3, 5, 9}

Second execution output:

删除前: {
    
    1, 2, 3, 5, 9}
删除后: {
    
    2, 3, 5, 9}

Execute it several times, we can find that pop() deletes 1 every time, because 1 is the smallest and is always ranked first.


1.2, plain characters

When the elements in the collection are "pure strings" , the collection cannot guarantee the ordering of the elements. Since pop() always deletes the first element, this situation seems to be a random deletion.

set1 = {
    
    'aaa', 'bbb', 'ccc', 'ddd'}

print('删除前:', set1)
set1.pop()
print('删除后:', set1)

First execution output:

删除前: {
    
    'ddd', 'bbb', 'aaa', 'ccc'}
删除后: {
    
    'bbb', 'aaa', 'ccc'}

Second execution output:

删除前: {
    
    'ddd', 'aaa', 'ccc', 'bbb'}
删除后: {
    
    'aaa', 'ccc', 'bbb'}

After multiple executions, it can be found that the ordering of elements in each collection is random, and pop() will also "randomly" delete the first element in the collection.


1.3 Mixed situation

When the elements in the collection have "mixed" combinations such as strings, integers, and tuples , the ordering of the elements will become random. Of course, pop() will still stubbornly delete the first element.

set1 = {
    
    (1, 2), (5, 6), (9, 8), (3, 4), 'aaa', 1, 2}

print('删除前:', set1)
set1.pop()
print('删除后:', set1)

output:

删除前: {
    
    'aaa', 1, 2, (1, 2), (3, 4), (9, 8), (5, 6)}
删除后: {
    
    1, 2, (1, 2), (3, 4), (9, 8), (5, 6)}

2. Empty collection

The collection using pop() must have elements, and an "empty collection" will report a KeyError.

set2 = set()

set2.pop()

output:

insert image description here

An 'empty collection' of this format will also report an error

set1 = {
    
    }

set1.pop()

output:

insert image description here

In fact, this is not a collection, but a dictionary

set1 = {
    
    }
print(type(set1))

output:

<class 'dict'>

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131840658
pop