a = [1,2,4]
b = [1,2,3,4,5,6]
想输出一个结果,c 是 a 和 b 的并集,但每个值要标明是否是交集,如
c = {"1" :True, "2" :True, "3" :False, "4": True, "5": False, "6":False}
应该怎么处理?😭
1
kkkkkrua 2018-12-10 18:18:48 +08:00
leetcode,交集
|
2
qwertyzzz 2018-12-10 18:24:43 +08:00
都是有序的话 我记得有个是 2 个从头互相比较的 好像要额外空间存储
|
3
AlisaDestiny 2018-12-10 18:41:53 +08:00 1
```python
a = [1,2,4] b = [1,2,3,4,5,6] u = set(a+b) r = {} for x in u: r[str(x)] = (x in a and x in b) print(r) ``` |
4
css3 OP |
5
nichijou 2018-12-10 20:58:15 +08:00 1
第一个依次存入哈希字典, key 是 number, value 是 false
然后遍历第二个,有的改 true, 没的存 key: false |
6
smdbh 2018-12-10 21:05:00 +08:00
L3 +1
|
7
zxcvsh 2018-12-10 22:03:17 +08:00 via iPhone
推荐 3 楼,5 楼也可行,但是 3 楼相对有优化
|
8
MrGba2z 2018-12-10 23:19:25 +08:00
>>> s1 = collections.Counter(a+b)
>>> s2 = collections.Counter(set(a+b)) >>> s1.subtract(s2) >>> s1 Counter({1: 1, 2: 1, 4: 1, 3: 0, 5: 0, 6: 0}) |