集合:
特征:1.由不同的元素组成
2.是无序的
3.集合中的元素必须是不可变类型(包括数字、字符串、元组、)
4.大括号 {} 括起来,,隔开每个元素
定义可变集合set:进行for循环输出,重复的元素忽略(去重但最后结果无序)
s=["hello","world","hello","alex"]
name=list(set(s))print(name)
View Code
定义不可变集合forzenset:只能定义一个元素
v=frozenset("dsafsd")print(v)
View Code
集合的关系运算:
交集:(intersection、&)
python_l=["aaa","sss","asd","mnb"]
linux_l=["sss","mnb","zxc"]
p_s=set(python_l