set () with the general procedure set of concepts

1. Concept

set is the set of basic data types a python, which is characterized by:

1. The elements will not be repeated (this nature may be utilized to remove duplicate elements)

2. disorder in the collection

3. The element may hash (int, str, bool, tuple)

The method as set represents a set of:

set ={}

2. General Operation

1. Increase

set.add () # the added data is added to the beginning of the collection

set.update () # iteration of the elements of disorder added to the collection

2. Delete

pop()

remove()

clear()

the set

Note here that pop () can not be specified variable otherwise it will error

3. Modify

Since the set elements of the collection is not indexed and can not locate elements in this set, and therefore modify the set collection can only delete and add

4. Query

set is an iterative object, you can use a for loop query

for el in s:

print (el)

5. Other operations

s1 = { "Liu can", "Zhao Si", "Long Hill Paper"}

s2 = { "Liu Kechang", "Pingxiang Zhang", "Long Hill Paper"}

1. intersection s1 & s2

Print (S1 & S2) # { "Long Hill Paper"}

or

print(s1.intersection(s2)) # {"皮长山"}

2. Set and S1 | S2 (disordered)

Print (S1 | S2) # { "Liu can", "Zhao Si", "skin long mountain", "Liu Kechang", "Pingxiang Zhang"}

or

print(s1.union(s2)) # {"刘能", "赵四", "皮长山","刘科长", "冯乡长"}

3.差集 s1 - s2

#将被差集合中存在于差集合的元素删除,即s1删除s1与s2的交集

print(s1 - s2) # {'赵四', '刘能'}

或者

print(s1.difference(s2)) # {'赵四', '刘能'}

4.反交集 s1 ^ s2

# 删除两个集合的交集然后s1与s2并集

print(s1 ^ s2) # {'冯乡长', '刘能', '刘科长', '赵四'}

或者

print(s1.symmetric_difference(s2)) # {'冯乡长', '刘能', '刘科长', '赵四'}

5.子集 s1 < s2

#判断集合s1是否是集合s2的子集

s1 = {"刘能", "赵四"}

s2 = {"刘能", "赵四", "皮长山"}

print(s1 < s2) # True

或者

print(s1.issubset(s2)) # True

6.超集 s1 > s2

#判断s1是否是s2的超集(是否包含s2)

print(s1 > s2) #False

或者

print(s1.issuperset(s2)) #False

7.使set集合变成一个可hash的

s = frozenset(["赵本山", "刘能", "皮长山", "长跪"])

dic = {s:'123'} # 可以正常使用了

print(dic) #{ frozenset(["赵本山", "刘能", "皮长山", "长跪"]) : '123'}

Guess you like

Origin www.cnblogs.com/zy740/p/11005662.html
set
set
Recommended