首页 > 编程语言 >python8 集合

python8 集合

时间:2022-11-13 12:23:06浏览次数:38  
标签:11 test2 print test set python8 集合

集合

介绍

  1. Python内置的数据结构
  2. 和列表、字典一样都属于可变类型的序列
  3. 集合是没有Value的字典,【即 只存在Key】
  4. 类型:set

创建

  1. 直接 {} 创建,用 ,号 分隔
  2. 内置函数set

例:

# 作者:咸瑜
# 代码时间:2022/11/12


# 创建集合
# 元素不允许重复 [重复的被替换]
# 那么其实内部也貌似是用hash来算的
test = {1, 2, 3, 4, 1, 5, 6, 7}
print(test, type(test))  # {1, 2, 3, 4, 5, 6, 7} <class 'set'>

# set函数的多种创建方法:
test = set(range(6))
print(test, type(test))  # {0, 1, 2, 3, 4, 5} <class 'set'>

print(set([6, 5, 4, 3]))  # {3, 4, 5, 6}
print(set((6, 5, 4, 3)))  # {3, 4, 5, 6}
print(set('咸瑜好帅'))  # {'好', '咸', '帅', '瑜'}
print(set({77, 88, 99, 11}))  # {88, 99, 11, 77}
print({}, type({}))  # {} <class 'dict'>
print(set(), type(set()))  # set() <class 'set'>

相关操作

判断

  1. is
  2. not in

新增

  1. add() 方法,一次添加一个元素
  2. updata方法,至少添加一个元素

删除

  1. remove() 方法,一次删除一个指定元素,不存在抛异常 KeyError
  2. discard() 方法,一次删除一个指定元素,不存在不抛异常!!!!
  3. pop()方法,一次只删除一个任意元素
  4. clear()方法,清空集合

例:

# 作者:咸瑜
# 代码时间:2022/11/13


# 集合的相关操作
test = {1, 2, 3, 4, 1, 5, 6, 7}

# 判断
print(1 in test)  # True
print(11 in test)  # False

print(1 not in test)  # False
print(11 not in test)  # True

# 添加
test.add(66)
print(test)

test.update({77, 88, 99})
print(test)

# 删除
test.remove(66)
# test.remove(0)  # 报异常
print(test)

test.discard(77)
test.discard(0)  # 不报异常
print(test)

test.pop() # 没有参数!!
test.pop() # 随机删除 删除那个元素我也不知道
print(test)

test.clear()
print(test)

集合间的关系

image-20221113110653401

只要是元素相等就是True,顺序不等也是True 例:

# 作者:咸瑜
# 代码时间:2022/11/13


# 集合的相关操作
test = {1, 2, 3}
test1 = {3, 2, 1}
test2 = {4}

print(test == test1)  # True
print(test != test1)  # False

# 判断test 是不是 其他集合 的超集
print(test.issuperset(test1))  # True
print(test.issuperset(test2))  # False

# 两个集合是否含有交集  有交集False 没交集为True
print(test.isdisjoint(test1), '?')  # False
print(test.isdisjoint(test2))  # True

集合的数学操作

image-20221113115023741

# 作者:咸瑜
# 代码时间:2022/11/13

test = {1, 2, 3}
test2 = {2, 4, 6}

# 交集操作
print(test.intersection(test2))  # 运行结果:{2}
print(test & test2)  # intersection 和 & 是一样的 # 运行结果:{2}
# 并集操作
print(test.union(test2))  # 运行结果: {1, 2, 3, 4, 6}
print(test | test2)  # union 和 | 一样效果 #运行结果: {1, 2, 3, 4, 6}
# 差集
print(test.difference(test2))  # 运行结果: {1,3}
print(test - test2)  # difference 和 - 效果一样 # 运行结果: {1,3}
# 对称差集
print(test.symmetric_difference(test2))  # 运行结果: {1, 3, 4, 6}
print(test ^ test2)  # symmetric_difference 和 ^ 效果一样,运行结果: {1, 3, 4, 6}

集合生成式

image-20221113120343820

# 作者:咸瑜
# 代码时间:2022/11/13

test = {i * i for i in range(1, 11)}
print(test) # {64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

列表、字典、元祖、集合 总结

image-20221113120725988

标签:11,test2,print,test,set,python8,集合
From: https://www.cnblogs.com/bi-hu/p/16885733.html

相关文章

  • Redis集合(Set)
    简介Redisset对外提供的功能与list类似是一个列表的功能,特殊之处在于set是可以自动排重的,当你需要存储一个列表数据,又不希望出现重复数据时,set是一个很好的选择,并且set提......
  • Redis有序集合Zset(sorted set)
    Redis有序集合zset与普通集合set非常相似,是一个没有重复元素的字符串集合。不同之处是有序集合的每个成员都关联了一个评分(score),这个评分(score)被用来按照从最低分到最高......
  • 凯斯轴承数据故障诊断深度学习迁移学习元学习代码开源集合
    网上down的代码,很多人是不是读起来费劲,分析起来比较麻烦,每个人有每个人的风格,因此代码需要别人帮着才能读明白!深度学习调参很麻烦,很多人苦于调参,一调就是一个月,结果却稀......
  • P3226 [HNOI2012]集合选数(状压 DP)
    P3226[HNOI2012]集合选数要求选出集合\(S\)满足如果\(x\)选择了,\(2x\)和\(3x\)都不能选择。求\(\{1,2,\dots,n\}\)的符合要求的子集数量。\(n\le10^5\)。......
  • 集合
    集合1.集合的理解和好处数组长度开始时必须指定,而且一旦指定,不能更改;保存的必须为同一类型的元素使用数组进行增加元素的示意代码-----比较麻烦集合可以......
  • 乱七八糟的模板集合
    字符串1.字符串哈希(可以用于直接比较字符串相同,找循环节[hash(l,r-x)==hash(l+x,r)可判定x为一个循环节])1//采用自然溢出2typedefunsignedlonglong......
  • 温故而知新——Java双列集合Map&Stream流
    总体目录:01-双列集合的特点02-Map集合常用的APIMap是顶层接口,常用方法如下:size()、isEmpty()、clear()方法容易理解;put()方法的细节:如果第一次添加元素,返回值为null......
  • Intent之对象传递(Serializable传递对象和对象集合)
    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。要求被传递的对象必须实现上述2种接口中的一种......
  • mybatis xml集合变量
    <sqlid="queryProductByCustomerCodeOrCustomerLineCodeWhere">IS_DELETED='0'<iftest="customerCode!=nullandcustomerCode!=''">andCUSTOMER_CODE=......
  • Java集合Map接口与Map.Entry学习
    Map接口不是Collection接口的继承。Map接口用于维护键/值对(key/valuepairs)。该接口描述了从不重复的键到值的映射。(1)添加、删除操作:Objectput(Objectkey......