写点什么

总算彻底搞懂 Python 集合了

作者:Jackpop
  • 2022 年 2 月 19 日
  • 本文字数:1807 字

    阅读完需:约 6 分钟

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


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


程序员宝藏库https://github.com/Jackpopc/CS-Books-Store

difference(set)

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


# example 1recepie_requirements = {'orange', 'chocolate', 'salt', 'pepper'}what_I_have = {'apple', 'banana','salt'}# I have to buy orange chocolate pepperprint('I have to buy', *recepie_requirements.difference(what_I_have))
# example2all_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'}
复制代码


发布于: 刚刚阅读数: 2
用户头像

Jackpop

关注

还未添加个人签名 2020.09.16 加入

公众号:平凡而诗意,微信:code_7steps,全网粉丝超20万,技术进阶、优质资源、实用工具,欢迎关注!

评论

发布
暂无评论
总算彻底搞懂Python集合了