python basis of a set of basic and supplementary (deleted through the bad, two-pass encoding)

set

  • Set: python data type

    :( important collection point) de-emphasis, a collection of relations

    Keywords: set

    Empty set: set ()

    s = {1,2,3,4}                
    print(type(s))   set
    lst = ["海绵","新力","海绵"]
    print(set(lst))    {'海绵', '新力'}    把列表转换成集合,输出结果  {'海绵', '新力'}

Element is required in the collection is immutable and unique, we will use it only to do the heavy
element of the set is immutable (hashable)

Has a unique feature was done to re-# disordered, variable, can be iterative

#给这一行代码进行去重 (1)
lst = [1,2,3,4,1,1,12,33,3,421,21,12,3]
print(list(set(lst)))
# 容器:能够存储数据的就是容器
# list,tuple,dict,set

Since it is the variable data type, it is possible to CRUD

1, by (2 types)

#    s = set()
#    s.update('alex')  # 迭代 添加#    
#    s.add("alex")       # 添加#     
print(s)

2, puncturing (three kinds)

# s = {"a","b",3,"c"}
# print(s.pop())    #随机删除 pop具有返回值  pop中无法指定索引
# s.remove("b")      #指定元素删除
# s.clear()            #清空 后的显示是空集合    set()
# print(s)

3, changes (two kinds)

# 改:    
# 1.先删后加    
# 2.转换数据类型进行修改

4, check (one kind)

# 查:
#     for循环
s = {"a","b",3,"c"}
for i in s:    
    print(i)  
Set relationship:

Intersection, union, difference, complement

python = {"海","孙","力","大圣"}
linux = {"海","大圣","meet","alex"}

print(python | linux)  # 并集              | == or   
结果:{'孙', '力', 'alex', '大圣', '海', 'meet'}
print(python & linux)  # 交集  shift + 7   & == and
结果:{'海', '大圣'}
print(python - linux)  # 差集
结果:{'孙', '力'}
print(linux - python)
结果:{'alex', 'meet'}
print(python ^ linux)  # 补集 反差集 对称集   shift + 6
结果:{'孙', '力', 'alex', 'meet'}
# python = {"海绵","孙一帆","岳新力","大圣"}
# linux = {"海绵","大圣"}
# 超级(父集):     判断python是不是linux的超集
# print(python > linux)  # 大于号  输出的是True  布尔值
#子集:      判断python是不是linux的超集
# print(linux < python)  # 小于号  输出的是True  布尔值

Guess you like

Origin www.cnblogs.com/zzsy/p/12216693.html