python练习题4.29找出不是两个数组共有的元素

给定两个整型数组,本题要求找出不是两者共有的元素。

输入格式:

输入分别在两行中给出两个整型数组,每行先给出正整数N(≤20),随后是N个整数,其间以空格分隔。

输出格式:

在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格。题目保证至少存在一个这样的数字。同一数字不重复输出。

代码如下:

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

x = list(map(int,input().split(" ")))
y = list(map(int,input().split(" ")))
n1 = x[0]
n2 = y[0]
a = x[1:]
b = y[1:]
c = a+b
d = sorted(set(c),key=c.index)

for i in range(0,len(d)):
    if d[i] not in a or d[i] not in b:
        if i != len(d)-1:
            print(d[i],end=" ")
        else:
            print(d[i])

# 10 3 -5 2 8 0 3 5 -15 9 100
# 11 6 4 8 2 6 -5 9 0 100 8 1

重点:去重,排序。set去重,sorted排序。指定key=c.index

然后就是循环判断了。整体来说程序不难,就我写的这个,大家应该都很好理解。

读书和健身总有一个在路上