首页 > 编程语言 > 详细

python基础知识笔记-集合

时间:2018-03-11 22:16:59      阅读:239      评论:0      收藏:0      [点我收藏+]

  集合使用花括号将元素之间用逗号隔开即可。集合的输出里不会出现相同的元素。集合中有并集、交集、差集等,以下为一些简单的表达:

   

def main():
    list1=[1,1,2,2,3,3]
    print(list1)
    set1={1,1,2,2,3,3}
    print(set1)
    set1.add(4)
    set1.add(5)
    print(set1)
    set2={1,3,5,7,9}
    print(set2)
    set3=set1 & set2
    #set3=set1.intersection(set2)
    print(set3)
    set3=set1 | set2
    #st3=set1.union(set2)
    print(set3)
    set3=set1-set2
    #set3=set1.difference(set2)
    print(set3)
    set3 = set2.difference(set1)
    print(set3)
    set3=set1 ^ set2
    #set3=set1.symmetric_difference(set2)
    print(set3)
    for val in set2:
        print(val)
    print(set2.pop())
    if 3 in set2:
        set2.remove(3)
    print(set2)
    print(set2<=set1)
    #print(set2.issubset(set1))
    print(set1>=set2)
    #print(set1.issuperset(set2))

if __name__ == ‘__main__‘:
    main()
结果:
[1, 1, 2, 2, 3, 3]
{1, 2, 3}
{1, 2, 3, 4, 5}
{1, 3, 5, 7, 9}
{1, 3, 5}
{1, 2, 3, 4, 5, 7, 9}
{2, 4}
{9, 7}
{2, 4, 7, 9}
1
3
5
7
9
1
{5, 7, 9}
False
False

  

python基础知识笔记-集合

原文:https://www.cnblogs.com/68xi/p/8546364.html

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