python中元组,列表,字典,集合删除项目方式的归纳

liftword3个月前 (01-29)技术文章24

九三,君子终日乾乾,夕惕若,厉无咎。

在使用python过程中会经常遇到这四种集合数据类型,今天就对这四种集合数据类型中删除项目的操作做个总结性的归纳。

  • 列表(List)是一种有序和可更改的集合。允许重复的成员。
  • 元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
  • 集合(Set)是一个无序和无索引的集合。没有重复的成员。
  • 词典(Dictionary)是一个无序,可变和有索引的集合。没有重复的成员。

一、元组

1、元组是不可更改的,因此您无法从中删除项目,但您可以完全删除元组(del)

thistuple = ("apple", "banana", "cherry")
del thistuple

print(thistuple)    # 这会引发错误,因为元组已不存在。

二、列表

有几种方法可以从列表中删除项目(remove,pop,del,clear)

1、remove() 方法删除指定的项目

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

2、pop() 方法删除指定的索引(如果未指定索引,则删除最后一项):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

3、del 关键字删除指定的索引:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

4、del 关键字也能完整地删除列表:

thislist = ["apple", "banana", "cherry"]
del thislist

5、clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

三、集合

有几种方法可以从集合中删除项目(remove,discard,pop,clear,del)

1、remove()

注释:如果要删除的项目不存在,则 remove() 将引发错误

thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)

2、discard()

注释:如果要删除的项目不存在,则 discard() 不会引发错误

thisset = {"apple", "banana", "cherry"}

thisset.discard("banana")

print(thisset)

3、您还可以使用 pop() 方法删除项目,但此方法将删除最后一项。请记住,set 是无序的,因此您不会知道被删除的是什么项目。

pop() 方法的返回值是被删除的项目。

thisset = {"apple", "banana", "cherry"}

x = thisset.pop()

print(x)

print(thisset)

4、clear() 方法清空集合

thisset = {"apple", "banana", "cherry"}

thisset.clear()

print(thisset)

5、del 彻底删除集合;如果此时再打印这个集合,就会报错。

thisset = {"apple", "banana", "cherry"}

del thisset

print(thisset)

四、字典

有几种方法可以从字典中删除项目(pop,popitem,clear,del)

1、pop() 方法删除具有指定键名的项目

thisdict =  {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
thisdict.pop("model")
print(thisdict)

2、popitem() 方法删除最后插入的项目(在 3.7 之前的版本中,删除随机项目)

thisdict =  {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
thisdict.popitem()
print(thisdict)

3、del 关键字删除具有指定键名的项目;del 关键字也可以完全删除字典(但是再打印这个字典时会报错)

thisdict =  {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
del thisdict["model"]
print(thisdict)
thisdict =  {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
del thisdict

print(thisdict) #this 会导致错误,因为 "thisdict" 不再存在。

4、clear() 关键字清空字典

thisdict =  {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
thisdict.clear()
print(thisdict)

相关文章

删除 Python 列表中的最后一个元素

列表是 Python 中一种重要的数据结构,在某些情况下,可能需要从列表中删除最后一个元素。在本文中,我们将介绍删除列表中最后一个元素的多种方法。方法 1:使用 pop() 方法删除列表中最后一个元素...

Python精讲:在Python中添加、修改、删除和更新字典元素详解

欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是《在Python中添加、修改和删除字典元素详解》。本知识点主要内容有:在Python中添加字典元素、在Python中修改字典元素、在Pyt...

在 Python 中从列表中删除换行符的多种方法

在这里,我们将学习使用不同的方法从 python 的列表中删除换行符。换行符使用特殊字符“\n”指定。每当我们使用字符“\n”时,它都会自动生成一个新行。在 Python 中,有许多内置函数可用于从列...

年近半百自学Python之列表元素的排序操作

列表元素的排序操作常见方式有两种:●调用sort()方法,列表中的所有元素默认按照从小到大的顺序进行排序,可以指定reverse=True进行降序排序。●调用内置函数sorted(),可以指定reve...

4.Python列表、元组、字典与集合(在python中列表,元组,字典的区别)

在Python编程中,列表、元组、字典和集合是四种非常重要的数据结构,它们各自扮演着不同的角色,为数据的存储和处理提供了极大的便利。特别是在量化交易中,这些数据结构的应用尤为关键:列表(List):主...