9.Set类型 - 集合

作者: HassGy | 来源:发表于2019-08-01 14:37 被阅读12次

set类型就是集合,集合本身时不可hash,即可变。
☑特点:元素之间具有不重复性,无序性,用"{}"括住可hash(不可变)的元素(int/str/tuple/bool)
☑算术运算:并,交,补集
☑两种类型:set普通集合,frozenset不可变集合
⚠集合没有索引,也是不可以定位一个元素,所以修改的方法只能先清空后添加。


内置函数

👉查询 -集合是一个可迭代对象,可以进行for循环迭代

x = set(('apple', 'banana', 'cherry'))
print(x)

for n in x:
    print(n)
--------------------------------------------------
{'cherry', 'apple', 'banana'}
cherry
apple
banana

👉增加 add()/update()

fruits = {'apple', 'banana', 'cherry'}
fruits.add('orange') 
print(fruits)
--------------------------------------------------------
x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
x.update(y) 
print(x)

👉修改 -没有直接修改,只能先删除,后添加。

s = {"刘嘉玲", '关之琳', "王祖贤","张曼⽟", "李若彤"}
# 把刘嘉玲改成赵本⼭
s.remove("刘嘉玲")
s.add("赵本⼭")
print(s)

👉删除 -pop()/remove()/clear()

fruits = {'apple', 'banana', 'cherry'}
fruits.pop() 
print(fruits)
---------------------------------------------------
fruits = {'apple', 'banana', 'cherry'}
fruits.remove('banana') 
print(fruits)
---------------------------------------------------
fruits = {'apple', 'banana', 'cherry'}
fruits.clear()
print(fruits)

其他操作函数 (&,|,-,^)都可以取代以下关键字来运算。

# 交集元素
x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
z = x.intersection(y)
print(z)

x = {'a', 'b', 'c'}
y = {'c', 'd', 'e'}
z = {'f', 'g', 'c'}
result = x.intersection(y, z)
print(result)
---------------------------------------------------------------
# 并集元素
x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
z = x.union(y) 
print(z)

x = {'a', 'b', 'c'}
y = {'f', 'd', 'a'}
z = {'c', 'd', 'e'}
result = x.union(y, z) 
print(result)
----------------------------------------------------------------
# 差集元素
x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
z = x.difference(y)
print(z)

x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
z = y.difference(x) 
print(z)
----------------------------------------------------------------
# 反差集
x = {'apple', 'banana', 'cherry'}
y = {'google', 'microsoft', 'apple'}
z = x.symmetric_difference(y) 
print(z)
-----------------------------------------------------------------
# 子集
x = {'a', 'b', 'c'}
y = {'f', 'e', 'd', 'c', 'b'}
z = x.issubset(y) 
print(z)

x = {'a', 'b', 'c'}
y = {'f', 'e', 'd', 'c', 'b', 'a'}
z = x.issubset(y) 
print(z)
------------------------------------------------------------------
# 超集
x = {'f', 'e', 'd', 'c', 'b', 'a'}
y = {'a', 'b', 'c'}
z = x.issuperset(y) 
print(z)

x = {'f', 'e', 'd', 'c', 'b'}
y = {'a', 'b', 'c'}
z = x.issuperset(y) 
print(z)

基本的数据类型已经讲完80%了,接下来会进行一些必要补充。

相关文章

  • 9.Set类型 - 集合

    set类型就是集合,集合本身时不可hash,即可变。☑特点:元素之间具有不重复性,无序性,用"{}"括住可hash...

  • iOS 深拷贝浅拷贝

    一,集合类型(NSArray、NSDictionary、NSSet等类型)与非集合类型(NSString等类型) ...

  • #python基础入门#04

    <组合数据类型> 集合类型序列类型(字符串,元组,列表)字典类型 集合类型:集合是多个元素的无序组合 特点:无序,...

  • redis 基本使用

    什么是redis 键值类型 String字符类型 map散列类型 list列表类型 set 集合类型 有序集合类型...

  • hive集合类型

    hive集合类型集合类型主要包括:array,map,struct等,hive的特性支持集合类型,这特性是关系型数...

  • 集合类型

    Collection 类型 Collection Type 数组(Array),字典(Dictionary),集合...

  • 集合类型

    上一篇:控制流当前篇:集合类型下一篇:基础大杂烩 这一课我们将学习更加抽象的数据类型:集合类型 ,集合类型是用来存...

  • 集合类型

    集合类型 Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值的集合。数组是有序的值集合。集合是唯...

  • 集合类型

    Swift提供了三种基本的几个类型,也就是我们熟知的数组Array,集合Set,字典dictionary,用来...

  • 集合类型

    1有序可重复 1.1Array数组 一旦定义,数组的大小不可变,其中的元素类型不可变 1.2 MutableLis...

网友评论

    本文标题:9.Set类型 - 集合

    本文链接:https://www.haomeiwen.com/subject/gzpqdctx.html