程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Finally, I understand the python collection thoroughly

編輯:Python
aggregate (set) It is an unordered sequence of non repeating elements , You can use braces  { }  perhaps  set()  Function to create a collection .
It is Python One of them is very important , And frequently used concepts . Whether in the daily development process , Or in the interview process will often encounter , Come today 11「 unknown 」 Collection usage of .
Programmer's treasure house :https://github.com/Jackpopc/CS-Books-Store

difference(set)

set_1.difference(set_2): This method helps you get the difference between two sets , let me put it another way , It allows you to get what exists in set_1 But not in a given set (set_2) The elements in .
# 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)  This set Method returns a containing set_1 Elements and set_2 Collection of elements of , Besides , The returned collection contains only unique elements .
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): Take the intersection of two sets , Return only those that exist at the same time set_1 and set_2 The elements in .
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): Check set_1 Whether all elements of exist in set_2 in .
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):  Check set_2 Whether all elements of exist in set_1 in .
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):  Check whether the two sets do not contain common elements .
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():  Delete a random element from a collection .
discard(value):  Delete the specified element in a collection , If the element doesn't exist , It doesn't cause mistakes .
remove(value):  Delete the specified element in a collection , If the element doesn't exist , It causes a mistake .
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():  Delete all elements in the collection .
countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

print(len(countries)) # 4

countries.clear()

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

copy

copy():  This method allows you to get a copy of the specified element set
countries = {'Morocco', 'UK', 'Spain', 'USA', 'UK'}

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved