随机产生2组10个数字列表
每个数字取值 [10 ,20 ]
统计20个数字中一共有多少个不同数字?
2组之间,不重复的数字有几个,分别是什么?
2组直接进行比较,重复的数字有几个?分别是什么?
import random s1 = set([random.randint(10,20) for _ in range(10)]) s2 = set([random.randint(10,20) for _ in range(10)]) print(s1,s2) a = s1 | s2 a1 = s1.union(s2) #使用函数名
print(a,a1) b = s1 ^ s2 b1 = s1.symmetric_difference(s2) #使用函数名
print(b,b1)
c = s1 & s2
c1 = s1.intersection(s2) #使用函数名
print(c,c1)
import random lst1 = [None] * 10 lst2 = [None] * 10 for i in range(10): lst1[i] = random.randint(10,20) lst2[i] = random.randint(10,20) print(lst1,lst2) s1 = set(lst1) s2 = set(lst2) print(s1,s2) a = s1 | s2 a1 = s1.union(s2) print(a,a1) b = s1 ^ s2 b1 = s1.symmetric_difference(s2) print(b,b1) c = s1 & s2 c1 = s1.intersection(s2) print(c,c1)
原文:https://www.cnblogs.com/alrenn/p/12620500.html