Python set

原创装载请注明出处:http://agilestyle.iteye.com/blog/2327559

set.py

l = [1, 3, 2, 1, 2, 3]
print(l)
s = set(l)
print(s)

# add(key)方法可以添加元素到set中,可以重复添加,但不会有效果
s.add(4)
print(s)

# remove(key)方法可以删除元素
s.remove(4)
print(s)

s1 = set([1, 2, 3])
s2 = set([2, 3, 4])
# {2, 3}
print(s1 & s2)
# {1, 2, 3, 4}
print(s1 | s2)

Console Output


 

猜你喜欢

转载自agilestyle.iteye.com/blog/2327559