python基础-----集合与函数

字符串

数字

字典

列表

元组

可变不可变:

1.可变:列表、字典

2.不可变:字符串、数字、元组

访问顺序:

1.直接访问:数字

2.顺序访问:字符串,列表,元组

3.映射:字典

存放元素个数:

容器类型:列表,元组,字典

原子:数字,字符串

#集合

  1.不同元素组成

  2.无序

  3.集合中元素必须是不可变类型(字符串,数字,元组)


#添加    add

#  随机删  pop

#   指定删    remove           (删除元素不产在会报错)

#discard    (删除元素不存在不会报错)

#求交集

python_1 = ['lcg','szw','zjw','lcg']
linux_1 = ['lcg','szw']
p_s=set(python_1)
l_s=set(linux_1)
print(p_s,l_s)
print(p_s.intersection(l_s))
print(p_s&l_s)

 #求并集

python_1 = ['lcg','szw','zjw','lcg']
linux_1 = ['lcg','szw','lsj']
p_s=set(python_1)
l_s=set(linux_1)
print(p_s,l_s)
print(p_s.union(l_s))
print(p_s|l_s)

#求差集

python_1 = ['lcg','szw','zjw','lcg']
linux_1 = ['lcg','szw','lsj']
p_s=set(python_1)
l_s=set(linux_1)
print(p_s,l_s)
print(p_s.difference(l_s))
print('差集',p_s-l_s)

猜你喜欢

转载自www.cnblogs.com/lishijie-/p/11625485.html
今日推荐