📜  设置时间复杂度python代码示例

📅  最后修改于: 2022-03-11 14:46:36.431000             🧑  作者: Mango

代码示例1
# empty set, avoid using {} in creating set or dictionary is created
x = set() 

# set {'e', 'h', 'l', 'o'} is created in unordered way
B = set('hello') 

# set{'a', 'c', 'd', 'b', 'e', 'f', 'g'} is created
A = set('abcdefg') 

# set{'a', 'b', 'h', 'c', 'd', 'e', 'f', 'g'} 
A.add('h')    

fruit ={'orange', 'banana', 'pear', 'apple'}

# True  fast membership testing in sets
'pear' in fruit      

'mango' in fruit     # False

A == B       # A is equivalent to B

A != B       # A is not equivalent to B

A <= B    # A is subset of B A = B    

A > B     # A is proper superset of B

A | B        # the union of A and B

A & B     # the intersection of A and B

A - B        # the set of elements in A but not B

A ˆ B        # the symmetric difference

a = {x for x in A if x not in 'abc'}   # Set Comprehension