首页 > 编程语言 >Python 集合(Sets)2

Python 集合(Sets)2

时间:2023-10-12 21:33:35浏览次数:43  
标签:示例 Python cherry print thisset Sets 集合 banana

访问项

您无法通过引用索引或键来访问集合中的项。但是,您可以使用for循环遍历集合项,或者使用in关键字检查集合中是否存在指定的值。

示例,遍历集合并打印值:

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

for x in thisset:
  print(x)

示例,检查集合中是否存在 "banana":

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

print("banana" in thisset)

Python - 添加集合项

一旦创建了集合,您就不能更改其项,但可以添加新项。要向集合添加一个项,请使用add()方法。

示例,使用add()方法向集合添加一个项:

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

thisset.add("orange")

print(thisset)

要将另一个集合中的项添加到当前集合中,请使用update()方法。

示例,将tropical中的元素添加到thisset中:

thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)

添加任何可迭代对象

update()方法中的对象不必是集合,可以是任何可迭代对象(元组、列表、字典等)。

示例,将列表的元素添加到集合中:

thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]

thisset.update(mylist)

print(thisset)

Python - 删除集合项

要删除集合中的项,可以使用remove()discard()方法。

示例,使用remove()方法删除 "banana":

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

thisset.remove("banana")

print(thisset)

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

示例,使用discard()方法删除 "banana":

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

thisset.discard("banana")

print(thisset)

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

您还可以使用pop()方法来删除一个项,但此方法将删除一个随机项,因此不能确定删除哪个项。pop()方法的返回值是已删除的项。

示例,使用pop()方法删除一个随机项:

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

x = thisset.pop()

print(x)

print(thisset)

注意:由于集合是无序的,因此在使用pop()方法时无法确定删除哪个项。

示例,clear()方法将清空集合:

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

thisset.clear()

print(thisset)

示例,del关键字将完全删除集合:

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

del thisset

print(thisset)

Python - 遍历集合

您可以使用for循环遍历集合项:

示例,遍历集合并打印值:

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

for x in thisset:
  print(x)

希望这些信息对您有所帮助!如果有任何问题或需要更多解释,请随时提问。

最后

为了方便其他设备和平台的小伙伴观看往期文章,链接奉上:

公众号搜索Let us Coding知乎开源中国CSDN思否掘金InfoQ简书博客园慕课51CTOhelloworld腾讯开发者社区阿里开发者社区

看完如果觉得有帮助,欢迎点赞、收藏关注

标签:示例,Python,cherry,print,thisset,Sets,集合,banana
From: https://www.cnblogs.com/xiaowange/p/17760631.html

相关文章

  • 代码随想录训练营的第二天(Python)| 977.有序数组的平方、209.长度最小的子数组
    977.有序数组的平方暴力求解(O(n+logn))classSolution:defsortedSquares(self,nums:List[int])->List[int]:returnsorted(i**2foriinnums)双指针(O(n))由于列表是单调递增的,元素平方后的最大值要么在最前面,要么在最后面classSolution:defsort......
  • 笨办法学Python3 习题33 while 循环
    while循环只要循环语句中的条件布尔值为True,就会不停的执行下面的代码块命令。while循环是无边界循环,forin循环是有边界循环和if语句的相似点都是检查一个布尔表达式的真假,if语句是执行一次,while循环是执行完跳回到while顶部,如此重复,直到布尔值为假False尽量少用w......
  • Python中的迭代器与生成器
    迭代:迭代是Python最强大的功能之一,是访问集合元素的一种方式。迭代器:迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。迭代器有两个基本的方法:iter()和next()。字符串,列表或元组对象都......
  • 2023.10.12python练习关于函数
    #让20以内的奇数写入函数里然后输出三遍defnumber():a=-1whilea<19:a+=2print(a,end="")b=1whileb<=3:b+=1number()print()#输出5次20以内的奇数并输出5次9*9乘法表,都写入一个函数里defwww():x,y=1,1z=......
  • python 删除es指定字段数据
    需求:删除es中指定IP相关的数据(remoteAddr:ip)日志格式fields.product:wantwords_zxxxx_feature@timestamp:Oct12,2023@18:56:39.000date_timeLocal:12/Oct/2023:18:56:39+0800ecs.version:1.12.0host.name:WebServer-ZJK-1httpReferer:https:/xxx/httpUserAge......
  • Cython加密python代码防止反编译
    本方法适用于Linux环境下:1.安装库Cythonpip3installCython==3.0.0a10 2.编写待加密文件:hello.pyimportrandomdefac():i=random.randint(0,5)ifi>2:print('success')else:print('failure') 3.编写加密脚本import......
  • simulink中调用python脚本
      command='test.py&';%后轴&:等待调用结束(command='test.py';%无后轴&:立即执行下一句[status,cmdout]=system(command,'-echo');    参考:详解MATLAB的函数system()和shell转义字符“感叹号”,并利用它们实现在MATLAB中运行(调用)外部exe程序_matlabsy......
  • python 链表
    fromtypingimportOptionalclassListNode:def__init__(self,val=0,next=None):self.val=valself.next=nextclassSolution:defpartition(self,head:Optional[ListNode],x:int)->Optional[ListNode]:lessListPre......
  • 【Python】FastAPI 使用python3.6+构建API的Web框架
    现代、快速(高性能)的Web框架,用于构建基于Python的 API;基于Starlette和Pydantic库构建而成官网:https://fastapi.tiangolo.com/ 1、安装#对于生产环境,还需要一个ASGI服务器,如Uvicorn或Hypercorn#>pipinstall"uvicorn[standard]"pipinstallfastapipipi......
  • python+playwright 学习-61 Playwright 和 Selenium 的区别是什么?
    前言最近有不少同学问到Playwright和Selenium的区别是什么?有同学可能之前学过selenium了,再学一个playwright感觉有些多余,可能之前有项目已经是selenium写的了,换成playwright需要时间成本,并且可能有未知风险。也有同学之前可能没学过selenium,现在正准备入手一个web......