今天为大家介绍的是Python中内置数据结构之一,集合,以下为学习参考代码。
# 机构:家里蹲
# 作者:青灬火
# 时间:2021/7/18 17:14
'''
集合
python语言提供的内置数据结构
与列表、字典一样都属于可变类型的序列
集合是没有value的字典
无序, 值不允许重复
'''
#集合的创建方式
#直接{}
s = {'python', 'hello', 'hello', 98}
print(s) #{'python', 'hello', 98}
print(type(s)) #<class 'set'>
#使用内置函数set()
s1 = set(range(6))
print(s1, type(s1)) #{0, 1, 2, 3, 4, 5} <class 'set'>
s2 = set([1, 2, 3, 4, 5, 5, 6])
print(s2, type(s2)) #{1, 2, 3, 4, 5, 6} <class 'set'>
s3 = set((1, 1, 2, 3, 4, 65))
print(s3, type(s3)) #{65, 1, 2, 3, 4} <class 'set'>
s4 = set('Python')
print(s4, type(s4)) #{'n', 'P', 'h', 't', 'o', 'y'} <class 'set'>
s5 = set({'python', 'hello', 'hello', 98})
print(s5, t