![图片[1]-python空集合如何表示-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5da03416bac09324.jpg)
python中,用set来表示一个无序不重复元素的序列。set的主要作用就是用来给数据去重。
可以使用大括号{ }或者set()函数创建集合,但是注意如果创建一个空集合必须用set()而不是{ },因为{}是用来表示空字典类型的。
set的集合的创建与使用
#1.用{}创建set集合
person={"student","teacher","babe",123,321,123}#同样各种类型嵌套,可以赋值重复数据,但是存储会去重
print(len(person))#存放了6个数据,长度显示是5,存储是自动去重.
print(person)#但是显示出来则是去重的
'''
5
{321,'teacher','student','babe',123}
'''
#空set集合用set()函数表示
person1=set()#表示空set,不能用person1={}
print(len(person1))
print(person1)
'''
set()
'''
#3.用set()函数创建set集合
person2=set(("hello","jerry",133,11,133,"jerru"))#只能传入一个参数,可以是list,tuple等类型
print(len(person2))
print(person2)
'''
5
{133,'jerry',11,'jerru','hello'}
'''原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容