集合 a = {x for x in 'abcdefghijkl' if x not in 'abc'} print (a) {'g', 'f', 'h', 'i', 'j', 'k', 'e', 'l', 'd'} 增加元素 thisset = set(("abc","def","ghi")) thisset.add("jkl") print(thisset) {'jkl', 'ghi', 'def', 'abc'} thisset.update({1,2,3}) print(thisset) {'jkl', 1, 2, 3, 'def', 'abc', 'ghi'} 移除元素 thisset.remove("abc") print(thisset) {'jkl', 1, 2, 3, 'def', 'ghi'} thisset.discard("jkl") print(thisset) {1, 2, 3, 'def', 'ghi'} x = thisset.pop() print(x) 1 判断元素是否存在 "abc" in thisset False 2 in thisset True 清空元素 thisset.clear() print(thisset) set()