Python集合的创建、使用和遍历操作是什么
Admin 2022-08-09 群英技术资讯 959 次浏览
这篇文章将为大家详细讲解有关“Python集合的创建、使用和遍历操作是什么”的知识,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。使用内置函数set()进行转化或者使用{}包括起来的,集合中的元素:无序性,互异性,确定性。
举例:
numbers=set(range(0,7))//使用内置函数进行转化 print(numbers) print(type(numbers))
输出:
{0, 1, 2, 3, 4, 5, 6}
<class 'set'>
互异性
fruit={'apple','orange','banana',"apple",'apple','orange','banana',"apple"}
print(fruit)
print(type(fruit))
输出:
{'apple', 'banana', 'orange'}
<class 'set'>
集合中的元素不能通过下标访问。
举例:
fruit =set({'apple',9,"axn","dbu",12})
print(fruit[2])

add() 函数
举例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.add("bc")
print(fruit)
输出:
{'bc', 'apple', 9, 12, 'dbu', 'axn'}
pop()函数
举例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.pop()
print(fruit)
输出:
{'apple', 9, 12, 'axn'}
1:remove()函数,若该元素不存在则会报错
举例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.remove("banana")
print(fruit)

fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.remove("apple")
print(fruit)
输出:
{'dbu', 'axn', 9, 12}
2:discard()函数,若指定元素不存在不会报错
举例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.discard("banana")
print(fruit)
输出:
{'dbu', 'apple', 9, 'axn', 12}
fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.discard("apple")
print(fruit)
输出:
{'dbu', 'axn', 9, 12}
if in/not in语句
举例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
if "apple" in fruit:
print("yes")
else:
print("NO")
if "banana" not in fruit:
print("YES")
else:
print("NO")
输出:
yes
YES
for循环
fruit =set({'apple',9,"axn","dbu",12,"apple"})
for i in fruit:
print(i,end=' ')
输出:
axn 9 apple 12 dbu
len()函数
举例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
print(len(fruit))
输出:
5//注意集合元素的唯一性特征
集合与字典:
s1=set({"name":"jason","age":19,"地址":"北京市"})
print(s1)
print(type(s1))
输出:
{'地址', 'name', 'age'}//只输出键名
<class 'set'>
举例:
s1={("name","jason","age",19,"地址","北京市"),12,34,0}
print(s1)
print(type(s1))
输出:
{0, 34, ('name', 'jason', 'age', 19, '地址', '北京市'), 12}
<class 'set'>
使用内置函数进行转化:
s1=set({"name","jason","age",19,"地址","北京市"})
print(s1)
print(type(s1))
输出:
{'age', 'jason', 19, '地址', '北京市', 'name'}
<class 'set'>
举例:
s2=set(["name","jason","age",19,"地址","北京市"]) print(s2) print(type(s2))
输出:
{'北京市', 'age', 'jason', 19, 'name', '地址'}
<class 'set'>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python 迭代器Iterator详情,迭代器可以帮助我们解决面对复杂的数据场景时,快速简便的获取数据,下文关于其详细介绍,需要的小伙伴可以参考一下
在训练yolo网络检测目标时,需要根据待检测目标的位置大小分布情况对anchor进行调整,使其检测效果尽可能提高,下面这篇文章主要给大家介绍了关于YOLOv5目标检测之anchor设定的相关资料,需要的朋友可以参考下
这篇文章主要为大家介绍了卷积神经Inception V3网络结构代码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要介绍了python collections模块如何使用的技巧,小编觉得collections模块的使用是比较实用的,因此分享给大家参考,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获。
python中的import语句是用来调用模块的,在模块定义好后,我们可以使用import语句来引入模块,常见的用法是与from搭配,语法格式为from...import...,这种结构更加清晰易懂。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008