Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python ลบอิลิเมนต์จาก Set ด้วย pop() (เวอร์ชัน 2)


บทความซ้ำกับ ID 1004106511 ใช้ pop() ลบอิลิเมนต์จาก set ทีละตัว

โค้ดตัวอย่าง

def remove_element(set1):
    print("NEW SET IS:")
    while set1:
        set1.pop()
        print(set1)

set1 = set([22, 120, 130, 115, 80, 9])
remove_element(set1)

ผลลัพธ์

NEW SET IS:
{9, 80, 115, 22, 120}
{80, 115, 22, 120}
{115, 22, 120}
{22, 120}
{120}
set()