列表的交并补差运算

题目要求:实现两个列表的交并补差运算

参考高中集合知识可知:
(均以A,B集合为例)
集合特点:无序,互异,确定
交集:两个集合的公共元素组成的集合 记作:A∩B
并集:包含两个集合的所有元素的集合 记作:A∪B
补集:A是B的一个子集,由B中所有不属于A的元素组成的集合,叫做子集A在B中的补集 记作:cBA
差集:(A - B) A中不含B中所有元素的集合

思路:(list1,list2)
1列表交集(intersect):两个列表中所有的公共元素所组成的列表,一一比对即可
2列表并集(union):先求出两个列表的公共部分list3,然后再将list1,list2中不在list3中的元素加入list3
3补集(complementary):首先判断是否能进行运算;然后在判断谁求谁的补集
4差集(dif):dif = complementary ( list1,intersect( list1, list2 ) )
实现代码:

# 写出列表的交,补,并,差集
# 求两个列表的交集 返回一个列表
def intersect(list1, list2):
    intersectlist = []
    for i in list1:
        for j in list2:
            if i == j:
                intersectlist.append(i)
                break
    intersectlist.sort()
    return intersectlist


# 求两个列表的并集
def union(list1, list2):
    list_sect = intersect(list1, list2)  # 先求两个列表的交集
    list_union = []  # 初始化一个list_union
    for num in list_sect:
        list_union.append(num)  # 将交集中所有元素放入list_union列表
    for i in list1:
        if i in list_sect:
            continue
        else:
            list_union.append(i)
    for i in list2:
        if i in list_sect:
            continue
        else:
            list_union.append(i)
    list_union.sort()
    return list_union


# 求两列表的补集
def complementary(list1, list2):
    comple = []
    list_intect = intersect(list1, list2)  # 取交集
    if list_intect != list1 and list_intect != list2:  # 交集不等于任何一个集合
        return '不存在包含与被包含关系'
    elif list_intect == list1:  # 交集等于list1
        for num in list2:
            if num in list_intect:
                continue
            else:
                comple.append(num)
        return comple
    else:
        return complementary(list2, list1)


# 求两个集合的差集A - B = comple(A,intersect(A,B))
def dif(list1, list2):
    return complementary(list1, intersect(list1, list2))


listunion = union([1, 2, 3, 4], [2, 3, 4, 5])
listintersect = intersect([1, 2, 3, 4], [2, 3, 4, 5])
listcomple = complementary([1, 2, 3, 4], [1, 2, 3, 4, 5])
list_dif = dif([1, 2, 3, 4, 7], [1, 2, 3, 4, 5])
print(listunion, listintersect, listcomple, list_dif)

!!本文仅供自己参考!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是用单链表实现集合的补差运算的代码。代码中使用了结构体表示单链表节点和集合元素: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; typedef struct Set { Node* head; int size; } Set; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; return newNode; } Set* createSet() { Set* newSet = (Set*)malloc(sizeof(Set)); newSet->head = NULL; newSet->size = 0; return newSet; } void addNode(Set* set, int data) { Node* newNode = createNode(data); newNode->next = set->head; set->head = newNode; set->size++; } void printSet(Set* set) { printf("{ "); Node* curr = set->head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("}\n"); } Set* intersection(Set* set1, Set* set2) { Set* newSet = createSet(); Node* curr1 = set1->head; while (curr1 != NULL) { Node* curr2 = set2->head; while (curr2 != NULL) { if (curr1->data == curr2->data) { addNode(newSet, curr1->data); break; } curr2 = curr2->next; } curr1 = curr1->next; } return newSet; } Set* unionSet(Set* set1, Set* set2) { Set* newSet = createSet(); Node* curr1 = set1->head; while (curr1 != NULL) { addNode(newSet, curr1->data); curr1 = curr1->next; } Node* curr2 = set2->head; while (curr2 != NULL) { Node* currNew = newSet->head; int exists = 0; while (currNew != NULL) { if (curr2->data == currNew->data) { exists = 1; break; } currNew = currNew->next; } if (!exists) { addNode(newSet, curr2->data); } curr2 = curr2->next; } return newSet; } Set* complement(Set* set1, Set* set2) { Set* newSet = createSet(); Node* curr1 = set1->head; while (curr1 != NULL) { Node* curr2 = set2->head; int exists = 0; while (curr2 != NULL) { if (curr1->data == curr2->data) { exists = 1; break; } curr2 = curr2->next; } if (!exists) { addNode(newSet, curr1->data); } curr1 = curr1->next; } return newSet; } Set* difference(Set* set1, Set* set2) { Set* newSet = createSet(); Node* curr1 = set1->head; while (curr1 != NULL) { Node* curr2 = set2->head; int exists = 0; while (curr2 != NULL) { if (curr1->data == curr2->data) { exists = 1; break; } curr2 = curr2->next; } if (!exists) { addNode(newSet, curr1->data); } curr1 = curr1->next; } curr1 = set2->head; while (curr1 != NULL) { Node* curr2 = set1->head; int exists = 0; while (curr2 != NULL) { if (curr1->data == curr2->data) { exists = 1; break; } curr2 = curr2->next; } if (!exists) { addNode(newSet, curr1->data); } curr1 = curr1->next; } return newSet; } int main() { Set* set1 = createSet(); Set* set2 = createSet(); addNode(set1, 1); addNode(set1, 3); addNode(set1, 5); addNode(set1, 7); addNode(set2, 2); addNode(set2, 4); addNode(set2, 6); addNode(set2, 8); printf("Set1: "); printSet(set1); printf("Set2: "); printSet(set2); Set* inter = intersection(set1, set2); printf("Intersection: "); printSet(inter); Set* uni = unionSet(set1, set2); printf("Union: "); printSet(uni); Set* com = complement(set1, set2); printf("Complement: "); printSet(com); Set* diff = difference(set1, set2); printf("Difference: "); printSet(diff); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值