python中的集合和数学中的集合是一样的,用来保存不重复的元素。也就是说集合中的元素是唯一的。
1。 使用{}创建
a = {1, 3, "java", 'python'}
print(a)
{1, 3, 'java', 'python'}
2。使用set()函数创建
a = set([1, 2, "java", 'python'])
print(a)
print("---------")
b = set("python")
print(b)
print('---------')
c = set((1, 2, 'java', 'python'))
print(c)
print('---------')
d = set(range(0, 10))
print(d)
{1, 2, 'java', 'python'}
---------
{'y', 'o', 't', 'n', 'h', 'p'}
---------
{1, 2, 'java', 'python'}
---------
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
注意:
1。 同一集合内不能出现可变的数据类型
a = {1, 2, [1, 2]}
print(a)
Traceback (most recent call last):
File "/Users/apple/Documents/重要文件/python3/python20.py", line 1, in
a = {1, 2, [1, 2]}
TypeError: unhashable type: 'list'
2。如果集合中出现相同的元素那么只会保留一个
a = {1, 2, 2}
print(a)
{1, 2}
由于集合中的元素是无序的所以无法像访问列表那样通过下标访问。 一般的做法是通过循环访问
a = {1, 2, 2}
for key in a:
print(key)
1
2
和其他类型一样可通过del关键字删除
a = {1, 2, 2}
print(a)
del a
print(a)
{1, 2}
Traceback (most recent call last):
File "/Users/apple/Documents/重要文件/python3/python20.py", line 4, in
print(a)
NameError: name 'a' is not defined
该方法不能添加可变的数据类型。 否则会报错
a = {1, 2, 2}
print(a)
a.add("java" )
print(a)
a.add(["python", "js"])
print(a)
{1, 2}
{1, 2, 'java'}
Traceback (most recent call last):
File "/Users/apple/Documents/重要文件/python3/python20.py", line 5, in
a.add(["python", "js"])
TypeError: unhashable type: 'list'
该方法删除元素时。如果元素不存在则会报错
a = {1, 2, 2}
print(a)
a.remove(1)
print(a)
a.remove( "java" )
print(a)
{1, 2}
{2}
Traceback (most recent call last):
File "/Users/apple/Documents/重要文件/python3/python20.py", line 5, in
a.remove( "java" )
KeyError: 'java'
该犯法当被删除元素不存在时不会报错
a = {1, 2, 2}
print(a)
a.discard(1)
print(a)
a.discard("java")
print(a)
{1, 2}
{2}
{2}
1。“&”交集。 取两个元素之间的公共元素
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a & b
print(c)
{1, 'java'}
2。 “|”并集。 取两个集合之间的全部元素
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a | b
print(c)
{1, 'c', 2000, 'java', 'js', 'python'}
3。“-” 差集。取一个集合中在另一个集合中不存在的元素
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a - b
print(c)
{2000, 'python'}
4 “^”对称差集。 取集合一和集合二中不属于集合一和集合二交集的元素
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a ^ b
print(c)
{2000, 'js', 'c', 'python'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
print(a)
a.clear()
print(a)
{'python', 1, 'java', 2000}
set()
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = b.difference(a)
print(c)
{'js', 'c'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
a.difference_update(b)
print(a)
{2000, 'python'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a.intersection(b)
print(c)
{1, 'java'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
a.intersection_update(b)
print(a)
{'java', 1}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a.isdisjoint(b)
print(c)
False
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a.issubset(b)
print(c)
False
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = b.issuperset(a)
print(c)
False
a = {"java", 'python', 1, 2000}
a.pop()
print(a)
{1, 'python', 'java'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a.symmetric_difference(b)
print(c)
{2000, 'js', 'c', 'python'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
a.symmetric_difference_update(b)
print(a)
{'c', 'python', 2000, 'js'}
a = {"java", 'python', 1, 2000}
b = {"java", 'js', 'c', 1}
c = a.union(b)
print(c)
{1, 2000, 'python', 'c', 'js', 'java'}
a = {"java", 'python', 1, 2000}
a.update([200, 3000])
print(a)
{'java', 1, 200, 2000, 'python', 3000}