总算把集合玩明白了!

集合(set)是一种无序的不重复元素序列,可以使用大括号  { }  或者 set()  函数创建集合。

它是Python中一个非常重要,且频繁用到的概念。无论是在日常开发过程中,还是在面试过程中都会经常遇到,今天就来11「不为人知」的集合用法。

difference(set)

set_1.difference(set_2) :这个方法帮助你获得两个集合之间的差异,换句话说,它让你获得存在于set_1中而不存在于给定集合(set_2)中的元素。

# example 1
recepie_requirements = {'orange', 'chocolate', 'salt', 'pepper'}
what_I_have = {'apple', 'banana','salt'}
# I have to buy orange chocolate pepper
print('I have to buy', *recepie_requirements.difference(what_I_have))

# example2
all_subscribers = {"aya", "john", "smith", "sparf", "kyle"}
admins = {"aya", "sparf"}
users = all_subscribers.difference(admins)
# {'kyle', 'smith', 'john'}
print(users)

union(set)

set_1.union(set_2) :(set_1 U set_2) 这个set方法返回一个包含set_1的元素和set_2的元素的集合,此外,返回的集合只包含唯一的元素。

admins = {'aya', 'sparf'}
users = {'aya','kyle', 'smith', 'john'}

all_subscribers = admins.union(users)

# {'smith', 'aya', 'sparf', 'kyle', 'john'}
print(all_subscribers)

intersection(set)

set_1.intersection(set_2) :取两个集合的交集,只返回同时存在于set_1和set_2中的元素。

shop = {'orange', 'pepper', 'banana', 'sugar'}
what_I_have = {'orange', 'sugar'}

# I should not buy {'orange', 'sugar'} because I have them!
print(f'I should not buy {shop.intersection(what_I_have)} because I have them!')

issubset()

set_1.issubset(set_2) :检查set_1的所有元素是否存在于set_2中。

nearest_library_books = {"the power of now", 'why we sleep', 'rich dad poor dad'}
necessary_books = {'atomic habits','the 48 laws of power', 'why we sleep'}

if necessary_books.issubset(nearest_library_books):
  print('yes, you can buy these books from your nearest library')
else:
  print('unfortunately, you have to go to another library')

# unfortunately, you have to go to another library

issuperset()

set_1.issuperset(set_2) : 检查set_2的所有元素是否存在于set_1中。

nearest_library_books = {"the power of now", 'why we sleep', 'rich dad poor dad'}
necessary_books = {'atomic habits','the 48 laws of power', 'why we sleep'}

if nearest_library_books.issuperset(necessary_books):
  print('yes, you can buy these books from your nearest library')
else:
  print('unfortunately, you have to go to another library')

# unfortunately, you have to go to another library

isdisjoint(set)

isdisjoint(set) : 检查这两个集合是否不包含共同的元素。

set_1 = {12, 38, 36}
set_2 = {4, 40, 12}
# means can set_1 element - set_2 element == 0 ?
can_substruction_be_zero = set_1.isdisjoint(set_2)
print(can_substruction_be_zero) # False

discard(value), remove(value), pop()

pop() : 从一个集合中删除一个随机元素。

discard(value) : 删除一个集合中的指定元素,如果该元素不存在,不会引发错误。

remove(value) : 删除一个集合中的指定元素,如果该元素不存在,则引发错误。

users = {"Aya Bouchiha", "John Doe", "Kyle Smith", "Nabo Snay"}
deleted_account = 'Aya Bouchiha'

users.discard(deleted_account)
users.discard('Hi!')
print(users) # {'Kyle Smith', 'John Doe', 'Nabo Snay'}

users.remove('Kyle Smith') 
print(users) # {'Nabo Snay', 'John Doe'}

users.pop()
print(users) # {'John Doe'}

users.remove('Hello!') # KeyError

clear()

clear() : 删除集合中所有元素。

countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

print(len(countries)) # 4

countries.clear()

print(countries) # set()
print(len(countries)) # 0

copy

copy() : 这个方法让你得到一个指定元素集的副本

countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

print(countries) # {'UK', 'Morocco', 'Spain', 'USA'}
print(countries.copy()) # {'UK', 'Morocco', 'Spain', 'USA'}

以上就是本次分享的所有内容,如果你觉得文章还不错,欢迎关注公众号:Python编程学习圈,每日干货分享,发送“J”还可领取大量学习资料,内容覆盖Python电子书、教程、数据库编程、Django,爬虫,云计算等等。或是前往编程学习网,了解更多编程技术知识。

关注公粽号:Python编程学习圈,可免费领取最新Python技术资料包。

23 声望
10 粉丝
0 条评论
推荐阅读
使用VScode的几点感受,对比Pycharm、Jupyter优劣势
之前一直是PyCharm+Jupyter的组合,能满足几乎所有的Python开发需求。最近我开始用vscode,发现很香。PyCharm适合做项目开发,或者平常写写脚本,算是全能型IDE。但PyCharm体积大,对硬件消耗厉害,不够轻便。Jup...

Python技术大本营阅读 476

封面图
数据结构与算法:二分查找
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...

白鲸鱼9阅读 5.4k

滚蛋吧,正则表达式!
你是不是也有这样的操作,比如你需要使用「电子邮箱正则表达式」,首先想到的就是直接百度上搜索一个,然后采用 CV 大法神奇地接入到你的代码中?

良许3阅读 1.5k

搭个ChatGPT算法模型,从哪开始?
最近 ChatGPT 很火,火到了各行各业。记得去年更多的还是码农最新体验后拿它搜代码,现在各行各业都进来体验,问它咋理财、怎么写报告和给小孩起名。😂 也因此让小傅哥在头条的一篇关于 ChatGPT 的文章都有了26万...

小傅哥6阅读 1.3k

封面图
程序员适合创业吗?
大家好,我是良许。从去年 12 月开始,我已经在视频号、抖音等主流视频平台上连续更新视频到现在,并得到了不错的评价。每个视频都花了很多时间精力用心制作,欢迎大家关注哦~考虑到有些小伙伴没有看过我的视频,...

良许3阅读 1.3k

PyCharm 激活破解教程, 2023 年 2 月亲测有用
本文分享一下PyCharm 2022.2.3 版本最新激活破解教程,注意不要使用太新的版本,都是 Jetbrains 产品,本文专门配上了 Pycharm 的图片,跟着下面教程一步一步来即可。

程序员徐公阅读 10.3k评论 1

Ubuntu20.04 从源代码编译安装 python3.10
Ubuntu 22.04 Release DateUbuntu 22.04 Jammy Jellyfish is scheduled for release on April 21, 2022If you’re ready to use Ubuntu 22.04 Jammy Jellyfish, you can either upgrade your current Ubuntu syste...

ponponon1阅读 4.6k评论 1

关注公粽号:Python编程学习圈,可免费领取最新Python技术资料包。

23 声望
10 粉丝
宣传栏