首页 > 编程语言 > 详细

Python12:集合

时间:2018-04-22 10:07:09      阅读:183      评论:0      收藏:0      [点我收藏+]

集合,就是一组去重的元素。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

设置list_1为集合,差打印。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_1,list_2)
print(list_1.intersection(list_2)) #print(list_1 & list_2)


print(list_1.union(list_2))  #print(list_1 | list_2)


print(list_1.difference(list_2)) # print(list_1 - list_2)

 

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8} {0, 2, 66, 4, 6, 8, 22}

{8, 2, 4, 6}

{0, 1, 2, 3, 4, 5, 6, 7, 8, 66, 22}

{1, 3, 5, 7}

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.intersection(list_2):两个集合的交集。

list_1.union(list_2):两个集合的并集。

list_1.difference(list_2):两个集合的差集。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_1.issubset(list_2)) # print(list_1 < list_2)

print(list_1.issuperset(list_2)) # print(list_1 > list_2)

 

输出:

False

False

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.issubset(list_2)判断list_1是否是list_2的子集。

list_1.issuperset(list_2):判断list_1是否是list_2的父集。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4]) 
print(list_1.symmetric_difference(list_2)) # print(list_1 ^ list_2)

输出:

{0, 1, 66, 7, 3, 22, 5}

 

Process finished with exit code 0

解释:“#”号后边的是等价操作。

list_1.symmetric_difference(list_2)两个集合的并集,去掉交集部分。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
print(list_2.isdisjoint(list_1))

 

输出:

False

 

Process finished with exit code 0

解释:

list_2.isdisjoint(list_1):如果两个集合没有交集,就返回True

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.add(123)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8, 123}

 

Process finished with exit code 0

解释:

list_1.add(123):增加集合元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.update([12,23,34])
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8, 34, 12, 23}

 

Process finished with exit code 0

解释:

list_1.update([12,23,34]):也是增加集合的元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.remove(6)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 7, 8}

 

Process finished with exit code 0

解释:

list_1.remove(6):删除集合中的元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.pop()
print(list_1)

 

输出:

{2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

list_1.pop():删除集合中的元素,默认删除开头。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
list_1.discard(123)
print(list_1)

 

输出:

{1, 2, 3, 4, 5, 6, 7, 8}

 

Process finished with exit code 0

解释:

list_1.discard(123):删除集合中的元素,同remove()不同的是,如果集合中没有这个元素,也不会报错,而remove则会报错。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
n = len(list_1)
print(n)

 

输出:

8

 

Process finished with exit code 0

解释:

n = len(list_1):集合的长度。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

list_1 = [1,2,3,4,5,6,6,7,8]
list_1 = set(list_1)
print(2 in list_1)

 

输出:

True

 

Process finished with exit code 0

解释:

print(2 in list_1)判断是否在集合中。

Python12:集合

原文:https://www.cnblogs.com/mclind/p/8905507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!