Python 在Sets中使用remove()和discard()

Python 在Sets中使用remove()和discard()

根据调查,全球使用最多的编程语言是Python。这显示了需要了解Python中使用的不同编程方法的必要性。Python使用不同方法存储所有的编程数据。一些不同的数据类型包括Sets、List和Dictionary。在本文中,我们将学习关于Python Sets以及如何在Python Sets中使用remove()和discard()函数。

Remove()

这个函数用于从集合中删除一个特定的元素。它从集合中删除指定的元素,然后显示修改后的输出。这个方法的唯一限制是它只能一次从指定的数据集中删除一个元素。我们可以通过示例来了解remove()函数的用法。首先看一下remove()函数的基本语法:

set.remove(data) #This is the basic form in which the remove() method is used to remove elements

让我们拿两种不同的情况来学习remove()函数在两种不同情况下的输出结果。

示例1:存在要删除的元素

Countries = {"India", "London", "Nigeria", "Australia", "Dubai", "Italy", "Kazakhstan", "Iran", "Turkey", "Switzerland"}  # The data set contains all the different names
Countries.remove("Australia")  #Specify using the remove() method the name to be removed from the data set
print(Countries)

输出

上述代码的输出结果如下:

{'Nigeria', 'Switzerland', 'Turkey', 'London', 'India', 'Dubai', 'Italy', 'Kazakhstan', 'Iran'}

示例2:要删除的元素不存在

Countries = {"India", "London", "Nigeria", "Australia", "Dubai", "Italy", "Kazakistan", "Iran", "Turkey", "Switzerland"}  # The data set contains all the different names other then Pakistan
Countries.remove("Pakistan")  #The remove() method displays an error when the element not present in datset is to be removed
print(Countries)

输出

上方代码的输出结果为:

KeyError: 'Pakistan'  #There will be no output displayed and instead of it such an error will be displayed

discard()

此方法使用了一种不同的输出显示方法。在这种方法中,如果要删除的元素不在数据集中,则输出永远不会显示为错误,数据将按原样显示,没有任何变化,不像remove()方法在元素不存在时会显示为错误。此方法的限制与先前的方法相同,每次只能删除一个元素。我们可以通过以下代码和示例更清楚地理解:

set.discard(data) #This syntax will always be common in which the element to be removed will be defined in the parentheses

示例1:删除元素的存在

Cities = {"Hyderabad", "Bangalore", "Mumbai", "Pune", "Ahmedabad", "Kolkata", "Nagpur", "Nashik", "Jaipur", "Udaipur", "Jaisalmer"} #Different Elements are present in the data set
Cities.discard("Kolkata") #If the element will be present in the data set it will be normally removed and the manipulated output will be displayed
print(Cities)

输出

上面示例的输出结果如下:

{'Jaisalmer', 'Hyderabad', 'Nagpur', 'Ahmedabad', 'Bangalore', 'Pune', 'Mumbai', 'Udaipur', 'Jaipur', 'Nashik'}

示例2:要删除的元素不存在

States = {"Hyderabad", "Bangalore", "Mumbai", "Pune", "Ahmedabad", "Kolkata", "Nagpur", "Nashik", "Jaipur", "Udaipur", "Jaisalmer"} #The element name to be discarded is not present in the data set
States.discard("Kumbhalgarh") #If the element will not be present in the data set then the output will display the same data set without any manipulation
print(States)

输出

以上示例的输出将如下所示:

{'Nagpur', 'Jaipur', 'Nashik', 'Hyderabad', 'Ahmedabad', 'Udaipur', 'Pune', 'Jaisalmer', 'Kolkata', 'Bangalore', 'Mumbai'}

两种方法的区别

对于这两种方法,从数据集中移除选择的元素是最终的目标,但它们的方法和基本原理是不同的。当使用replace()函数时,如果要移除的元素不存在于提供的数据中,结果将不会被呈现出来,而是会显示一个错误。然而,当使用discard()方法时,情况就不同了。如果在使用discard()函数时要移除的元素不存在于数据中,输出将会显示原始的数据,不做任何修改,并且不会显示任何错误。

因此,这两种方法都有各自特定的用途和输出方式,它们根据需要在程序中使用。例如,如果您不确定数据中是否存在特定元素并且不希望在运行代码时出现任何错误,请使用discard()方法,这样即使元素不存在,输出也可以正常显示,而不会出现任何错误。

结论

Python是一种被全球程序员广泛使用的有用的编程语言,用于不同的目的。要成为一个高效和成功的程序员,有必要了解在较短时间内执行不同任务的不同方法。这是成为一个成功的Python开发人员并能高效快速地工作的关键。

本文展示了remove()和discard()方法的不同用途,以及它们的示例,以便为用户提供更清晰的参考,任何读者都可以将本文作为了解更多关于remove()和discard()方法的参考。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程