Python set 集合用法介绍
2017-08-08
1. set 介绍
Python set 是一个没有重复元素的无序集合,集合里面每个元素都是唯一的,它支持并集,交集,差集等操作
2. 创建集合
2.1 用花括号字面量定义集合,集合元素用逗号分隔,即使创建集合时元素出现多次,也只会保留一个
#!/usr/bin/env python
# coding: utf-8
nums = { 1, 2, 2, 2, 3, 4 }
print(type(nums))
print(nums)
The example creates a Python set with literal notation.
nums = { 1, 2, 2, 2, 3, 4 }
执行与输出
$ ./set_literal.py
<class 'set'>
{1, 2, 3, 4}
color = [“red”, “blue”]
myset = set(color)
print(myset)
执行与输出
```bash
$ ./set_from_list.py
{'red', 'blue'}
#!/usr/bin/env python
# coding: utf-8
# file: set_in.py
words = { "red", "green", "blue", "black", "white" }
word = 'green'
if word in words:
print("{0} is present in the set".format(word))
else:
print("{0} is not present in the set".format(word))
word = 'hello'
if word not in words:
print("{0} is not present in the set".format(word))
else:
print("{0} is present in the set".format(word))
执行与输出
$ ./set_in.py
green is present in the set
hello is not present in the set
#!/usr/bin/env python
# coding: utf-8
# file: set_builtins.py
nums = { 21, 11, 42, 29, 22, 71, 18 }
print(nums)
# 获取集合长度
print("Number of elements: {0}".format(len(nums)))
# 获取集合最小值,最大值
print("Minimum: {0}".format(min(nums)))
print("Maximum: {0}".format(max(nums)))
# 计算和
print("Sum: {0}".format(sum(nums)))
print("Sorted elements:")
# 创建一个排序列表
print(sorted(nums))
执行与输出
$ ./set_builtins.py
{71, 42, 11, 18, 21, 22, 29}
Number of elements: 7
Minimum: 11
Maximum: 71
Sum: 214
Sorted elements:
[11, 18, 21, 22, 29, 42, 71]
#!/usr/bin/env python
# coding: utf-8
# file: set_iter.py
words = { "spring", "table", "cup", "bottle", "coin" }
for word in words:
print(word)
执行与输出
$ ./set_iter.py
table
cup
coin
spring
bottle
集合使用 add 添加元素到当前集合, 使用 update 方法添加一个或多个集合到当前集合
#!/usr/bin/env python
# coding: utf-8
# file: set_add_update.py
words = { "spring", "table", "cup", "bottle", "coin" }
words.add("coffee")
print(words)
words2 = { "car", "purse", "wind" }
words3 = { "nice", "prime", "puppy" }
words.update(words2, words3)
print(words)
执行与输出
$ ./set_add_update.py
{'spring', 'bottle', 'cup', 'coin', 'purse', 'wind', 'nice', 'car',
'table', 'prime', 'puppy'}
5. 集合删除列表元素
有两个删除列表元素的方法: remove() 和 discard(). remove() 删除元素时,如果元素不在集合里,会抛出异常, discard() 删除元素时,如果元素不在集合里,则什么也不做
#!/usr/bin/env python
# coding: utf-8
# file: set_remove.py
words = { "red", "green", "blue", "black", "white"}
words.discard("red")
words.discard("green")
print(words)
words.remove("blue")
try:
words.remove("hello")
except KeyError as e:
pass
print(words)
执行与输出
$ ./set_remove.py
{'blue', 'black', 'white'}
{'black', 'white'}
pop() 删除并返回任意元素. clear() 删除集合所有元素
#!/usr/bin/env python
# coding: utf-8
# file: set_remove2.py
words = { "red", "green", "blue", "black", "white"}
print(words.pop())
print(words.pop())
print(words)
words.clear()
print(words)
输出
$ ./set_remove2.py
green
blue
{'black', 'white'}
set()
#!/usr/bin/env python
# coding: utf-8
# file: set_oper1.py
set1 = { 'a', 'b', 'c', 'c', 'd' }
set2 = { 'a', 'b', 'x', 'y', 'z' }
print("Set 1:", set1)
print("Set 2:", set2)
print("intersection:", set1.intersection(set2))
print("union:", set1.union(set2))
print("difference:", set1.difference(set2))
print("symmetric difference:", set1.symmetric_difference(set2))
intersection() 方法返回 set1,set2 交集(两个集合共有元素)
print("intersection:", set1.intersection(set2))
The intersection() method carries out the intersection operation, which returns elements that are both in set1 and set2.
union() 方法返回 set1,set2 并集(两个集合的所有元素)
print("union:", set1.union(set2))
difference() 返回在集合 set1,没在集合 set2 的元素
print("difference:", set1.difference(set2))
symmetric_difference() 异或函数返回两个集合的非共有元素
print("symmetric difference:", set1.symmetric_difference(set2))
输出结果
$ ./set_oper1.py
Set 1: {'c', 'b', 'a', 'd'}
Set 2: {'y', 'b', 'a', 'x', 'z'}
intersection: {'b', 'a'}
union: {'b', 'a', 'z', 'c', 'x', 'y', 'd'}
difference: {'c', 'd'}
symmetric difference: {'z', 'c', 'x', 'y', 'd'}
7.2 集合支持操作符,与 &,或 |,非 ~ ,异或 ^,如下例
#!/usr/bin/env python
# coding: utf-8
# file: set_oper2.py
set1 = { 'a', 'b', 'c', 'c', 'd' }
set2 = { 'a', 'b', 'x', 'y', 'z' }
print("Set 1:", set1)
print("Set 2:", set2)
print("intersection:", set1 & set2)
print("union:", set1 | set2)
print("difference:", set1 - set2)
print("symmetric difference:", set1 ^ set2)
8. 子集与超集的判断方法
- 当 set1 包含 set2 的所有元素,set1 是 set2 的超集,set2 是 set1 的子集
#!/usr/bin/env python
# coding: utf-8
# file: subset_superset.py 示例代码
set1 = { 'a', 'b', 'c', 'd', 'e' }
set2 = { 'a', 'b', 'c' }
set3 = {'x', 'y', 'z' }
if set2.issubset(set1):
print("set1 is a subset of set2")
if set1.issuperset(set2):
print("set1 is a superset of set2")
if set2.isdisjoint(set3):
print("set2 and set3 have no common elements")
判断 set2 是否是 set1 子集
if set2.issubset(set1):
print("set1 is a subset of set2")
判断 set1 是否是 set2的超集
if set1.issuperset(set2):
print("set1 is a superset of set2")
判断 set2 与 set3 没有相同元素
if set2.isdisjoint(set3):
print("set2 and set3 have no common elements")
输出
$ ./subset_superset.py
set1 is a subset of set2
set1 is a superset of set2
set2 and set3 have no common elements
9. Python 使用 frozenset() 创建不可变 set
修改不可见列表会抛出异常
>>> s = frozenset(('hello', 'world'))
>>> s
frozenset({'hello', 'world'})
>>> s.add('yes')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'