汇合(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,爬虫,云计算等等。或是返回编程学习网,理解更多编程技术常识。