快捷搜索:  汽车  科技

python集合的几种创建方式总结(python中元组列表字典)

python集合的几种创建方式总结(python中元组列表字典)thislist = ["apple" "banana" "cherry"] thislist.pop() print(thislist)3、del 关键字删除指定的索引:thislist = ["apple" "banana" "cherry"] thislist.remove("banana") print(thislist)2、pop() 方法删除指定的索引(如果未指定索引,则删除最后一项):thistuple = ("apple" "banana" "cherry") del thistuple print(thistuple) # 这会引发错误,因为元组已不存在。二、列表有几种方法可以从列表中删除项目(

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

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

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

一、元组

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

thistuple = ("apple" "banana" "cherry") del thistuple print(thistuple) # 这会引发错误,因为元组已不存在。

python集合的几种创建方式总结(python中元组列表字典)(1)

二、列表

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

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

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

python集合的几种创建方式总结(python中元组列表字典)(2)

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

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

python集合的几种创建方式总结(python中元组列表字典)(3)

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

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

python集合的几种创建方式总结(python中元组列表字典)(4)

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

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

python集合的几种创建方式总结(python中元组列表字典)(5)

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

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

python集合的几种创建方式总结(python中元组列表字典)(6)

三、集合

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

1、remove()

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

thisset = {"apple" "banana" "cherry"} thisset.remove("banana") print(thisset)

python集合的几种创建方式总结(python中元组列表字典)(7)

2、discard()

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

thisset = {"apple" "banana" "cherry"} thisset.discard("banana") print(thisset)

python集合的几种创建方式总结(python中元组列表字典)(8)

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

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

thisset = {"apple" "banana" "cherry"} x = thisset.pop() print(x) print(thisset)

python集合的几种创建方式总结(python中元组列表字典)(9)

4、clear() 方法清空集合

thisset = {"apple" "banana" "cherry"} thisset.clear() print(thisset)

python集合的几种创建方式总结(python中元组列表字典)(10)

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

thisset = {"apple" "banana" "cherry"} del thisset print(thisset)

python集合的几种创建方式总结(python中元组列表字典)(11)

四、字典

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

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

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

python集合的几种创建方式总结(python中元组列表字典)(12)

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

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

python集合的几种创建方式总结(python中元组列表字典)(13)

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

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

python集合的几种创建方式总结(python中元组列表字典)(14)

thisdict = { "brand": "Porsche" "model": "911" "year": 1963 } del thisdict print(thisdict) #this 会导致错误,因为 "thisdict" 不再存在。

python集合的几种创建方式总结(python中元组列表字典)(15)

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

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

python集合的几种创建方式总结(python中元组列表字典)(16)

猜您喜欢: