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

Python collections and their related operations

編輯:Python

Set definition

# coding:utf-8
if __name__ == '__main__':
''' example 1: adopt set(iterable) Define a collection ,iterable Is an iteratable object such as list,dict,tuple When iterable Is a tuple or list , Make a collection of all its elements , If the element contains list False report ( example 1-1) When iterable When is a dictionary , Owned by key Make up a collection example 2: adopt { Elements 1, Elements 2,......} Define a collection , Nor can its elements be list example 3:set() Empty sets can be defined , however {} no way ,{} It's empty dict example 4: The elements in the set do not repeat , disorder '''
# example 1
a = set([1, 2, 3, 4])
print(a) # {1, 2, 3, 4}
print(type(a)) # <class 'set'>
b = set((1, 2, 3, 4))
print(b) # {1, 2, 3, 4}
print(type(b)) # <class 'set'>
c = set({
'name': 'xie', 'age': 27})
print(c) # {'name', 'age'}
print(type(c)) # <class 'set'>
# example 1-1
a1 = set([1, (1, 3)])
# a2 = set([1, [1]]) Error
a3 = set((1, (1, 3)))
# a4 = set((1, [1])) Error
# example 2
d = {
1, 2, 3}
print(d)
e = {
(1, 2), (3, 4)}
print(e)
# f = {[1]} Because there are no changeable elements in the set , Therefore, an error is reported 
# example 3
g = set()
h = {
}
print(type(g)) # <class 'set'>
print(type(h)) # <class 'dict'>
# example 4
i = {
1, 1, 1, 2}
print(i) # {1, 2}

Difference set | intersection | Combine

# coding:utf-8
if __name__ == '__main__':
''' example 1:a_set.difference(b_set) Get by a Exists in collection ,b A collection of elements that do not exist in the collection ( Difference set ) example 2:a_set.intersection(b_set) Get by a Exists in collection ,b A collection of elements that also exist in a collection ( intersection ) example 3:a_set.union(b_set) Get by a All elements in the collection are related to b A collection of all elements in a collection ( Combine ) '''
# example 1
a = {
1, 2, 3}
b = {
3, 4, 5}
difference = a.difference(b)
print(difference) # {1,2}
# example 2
intersection = a.intersection(b)
print(intersection) # {3}
# example 3
union = a.union(b)
print(union) # {1, 2, 3, 4, 5}

The addition and modification of sets

# coding:utf-8:
if __name__ == '__main__':
''' example 1:set.add(element) Add elements to the collection element,element It can't be list,set type example 2:set.update(iterable) Modify set iterable Only support list,tuple,string When iterable Is a list or tuple ( If it contains list An error is reported for a type element ), Add all its elements to the collection When iterable When it's a string , Add the string character by character to the set '''
a = set()
print(a) # set()
# example 1
a.add('xie')
print(a) # xie
# a.add({1, 2}) Error
# a.add([1, 2]) Error
# example 2
# update tuple
a1 = set()
a1.update((1, 2, 3))
print(a1) # {1, 2, 3}
# update list
a2 = set()
a2.update([1, 2, 3])
print(a2) # {1, 2, 3}
# update str
a3 = set()
a3.update('123')
print(a3) # {'3', '2', '1'}

Element deletion and emptying

# coding:utf-8:
if __name__ == '__main__':
''' example 1:set.remove(element) Remove elements example 2:set.clear() Empty to get an empty set '''
# example 1
example = {
'a', 'b', 'c'}
example.remove('b')
print(example) # {'c', 'a'}
# example 2
example = {
'a', 'b'}
example.clear()
print(example) # set()

Determine whether there is no intersection between two sets ( There are no identical elements )

# coding:utf-8:
if __name__ == '__main__':
''' example 1:a_set.isdisjoint(b_set) Determine whether there is no intersection between two sets ( There are no identical elements ) return True Represents no intersection return False Represents an intersection '''
# example 1
a_set = {
'a', 'b', 'c'}
b_set = {
'a'}
c_set = {
'e'}
print(a_set.isdisjoint(b_set)) # False
print(a_set.isdisjoint(c_set)) # True

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