# Python集合

a=set()

print(type(a),a)

set()

a=[1,2,3,4,5]

print(type(a),a)

b=set(a)

print(type(b),b)

[1, 2, 3, 4, 5]

a=1

b=2

c=set()

print(type(c),c)

a=set([1,2,3,4,5,6])

print(type(a),a)

a={1,1,1,2,3,4}

print(type(a),a)

a={"a":1,"b":2}

print(type(a),a)

{'a': 1, 'b': 2}

a=set()

print(a)

a=set({1,2,3,4,5})

a.update([3,6,7])

print(a)

a=set({1,2,3,4,5})

a.remove(1)

print(a)

a=set({1,2,3,4,5})

a.pop()

print(a)

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a.union(b))

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a|b)

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a.intersection(b))

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a&b)

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a.difference(b))

print(b.difference(a))

a=set({1,2,3,4,5})

b=set({9,8,7,6,5})

print(a-b)

print(b-a)

a=set({1,2,3,4,5})

b=set({1,2,3})

print(b.issubset(a))

True

Issubset()函数就是来求是不是子集的问题。

a=set({1,2,3,4,5})

b=set({1,2,3})

print(b.issubset(a))

print(b

print(b

print(a.issubset(b))

print(a

print(a

True

True

True

False

False

False

a=set({1,2,3,4,5})

b=set({1,2,3,4,5})

print(b.issubset(a))

print(b

print(b

print(a.issubset(b))

print(a

print(a

True

True

False

True

True

False

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180829G07M5E00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2022-01-27

2022-01-27

2022-01-27

2018-07-02

2018-06-28

2018-06-05

2022-01-27

2022-01-27

2022-01-27