推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
css3
V2EX  ›  Python

小白问个问题

  •  
  •   css3 · Dec 10, 2018 · 2294 views
    This topic created in 2735 days ago, the information mentioned may be changed or developed.
    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}
    

    应该怎么处理?😭

    9 replies    2018-12-11 10:35:41 +08:00
    kkkkkrua
        1
    kkkkkrua  
       Dec 10, 2018
    leetcode,交集
    asLw0P981N0M0TCC
        2
    asLw0P981N0M0TCC  
       Dec 10, 2018
    都是有序的话 我记得有个是 2 个从头互相比较的 好像要额外空间存储
    AlisaDestiny
        3
    AlisaDestiny  
       Dec 10, 2018   ❤️ 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)
    ```
    css3
        4
    css3  
    OP
       Dec 10, 2018
    @kkkkkrua @qwertyzzz @AlisaDestiny

    感谢各位☺
    nichijou
        5
    nichijou  
       Dec 10, 2018   ❤️ 1
    第一个依次存入哈希字典, key 是 number, value 是 false

    然后遍历第二个,有的改 true, 没的存 key: false
    smdbh
        6
    smdbh  
       Dec 10, 2018
    L3 +1
    zxcvsh
        7
    zxcvsh  
       Dec 10, 2018 via iPhone
    推荐 3 楼,5 楼也可行,但是 3 楼相对有优化
    v2gba
        8
    v2gba  
       Dec 10, 2018
    >>> 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})
    css3
        9
    css3  
    OP
       Dec 11, 2018
    @nichijou @smdbh @zxcvsh @MrGba2z
    多谢😀
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2895 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 05:47 · PVG 13:47 · LAX 22:47 · JFK 01:47
    ♥ Do have faith in what you're doing.