美文网首页
基于python的集合运算

基于python的集合运算

作者: 潇洒坤 | 来源:发表于2018-10-02 00:07 被阅读86次

    两个集合的运算有:交集、并集、差集
    分别对应的操作符:& | ^

    test_list1 = [1, 2, 3 , 4]
    test_list2 = [3, 4, 5, 7]
    test_set1 = set(test_list1)
    test_set2 = set(test_list2)
    print(test_set1 & test_set2)
    print(test_set1 | test_set2)
    print(test_set1 ^ test_set2)
    

    上面一段代码的运行结果如下:

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

    相关文章

      网友评论

          本文标题:基于python的集合运算

          本文链接:https://www.haomeiwen.com/subject/fwrooftx.html