推荐学习书目
› 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
lovebeyondalways
V2EX  ›  Python

关于判断两个文本的内容

  •  
  •   lovebeyondalways ·
    piaokorg · Oct 7, 2016 · 4449 views
    This topic created in 3678 days ago, the information mentioned may be changed or developed.

    求教大神 文本 A 如下
    176.123.45.78
    46.64.117.70
    53.43.43.60
    文本 B
    fdiaofjao 176.123.45.78
    hfoisjojo 46.64.117.70
    fdsjfoaos 63.65.32.65
    请问怎么判断文本 A 的内容被包含于于文本 B

    18 replies  •  2016-10-08 15:47:32 +08:00
    lovebeyondalways
    lovebeyondalways
    OP
    Oct 7, 2016
    1
    想做的是如果文本 A 有 ip 存在于文本 B 就将它输出
    zhidian
    zhidian
    Oct 7, 2016
    2
    选你能用的编程语言, 读入文本 A, 扫描出所有的 IP, 存入 dict 中, 读入文本 B,
    依次扫描, 遇到 ip, 如果 ip 在 dict 中, 则输出之. that's easy.
    northisland
    northisland
    Oct 7, 2016
    4
    N ( A )数量个 String Searching 问题(Substring)

    (我记得最佳解法是用 KMP 算法)


    欢迎拍砖~
    northisland
    northisland
    Oct 7, 2016
    5
    后悔了,这是规则文本。

    提取 IP ,搜索 IP 查找表,就可以了。
    ldbC5uTBj11yaeh5
    ldbC5uTBj11yaeh5
    Oct 7, 2016
    6
    comm -12 a.txt <(awk '{print $2}' b.txt)
    ldbC5uTBj11yaeh5
    ldbC5uTBj11yaeh5
    Oct 7, 2016
    7
    靠,看错了,居然是 python 节点。
    (要啥自行车,用 shell 比 python 要快哒。)
    lovebeyondalways
    lovebeyondalways
    OP
    Oct 7, 2016 via Android
    8
    @jigloo 大哥 不好意思 我需要 Python 哈哈
    lovebeyondalways
    lovebeyondalways
    OP
    Oct 7, 2016 via Android
    9
    @jigloo 因为这段代码要 放进项目用
    ldbC5uTBj11yaeh5
    ldbC5uTBj11yaeh5
    Oct 7, 2016❤️1
    10
    @lovebeyondalways 呵呵,我就喜欢 oneliner , 拿去自己改吧改吧 (手动滑稽

    python -c 'import sys; any(sys.stdout.write(i) for i in set(open(sys.argv[1])).intersection((x.split(" ")[1] for x in open(sys.argv[2]))))' a.txt b.txt
    northisland
    northisland
    Oct 7, 2016 via Android
    11
    lookup_tb = dict( (e, 1) for e in A)

    for b in B:
    if b[b.rfind(' ')+1: ].strip() in lookup_tb:
    print 'b'
    lovebeyondalways
    lovebeyondalways
    OP
    Oct 7, 2016
    12
    @northisland 发送感谢
    lovebeyondalways
    lovebeyondalways
    OP
    Oct 7, 2016
    13
    @jigloo 感谢已发送
    owt5008137
    owt5008137
    Oct 7, 2016 via Android❤️1
    14
    @northisland KMP 是单个匹配,多个匹配可以试试 AC 自动机。
    不过规则文本的话更简单的优化方式茫茫多
    orange88
    orange88
    Oct 7, 2016 via Android
    15
    用 pandas 可以搞定
    lululau
    lululau
    Oct 7, 2016
    16
    grep -vFf <(cut -d ' ' -f 2 B) A
    SlipStupig
    SlipStupig
    Oct 7, 2016
    17
    python set.difference()

    difference(...)
    | Return the difference of two or more sets as a new set.
    |
    | (i.e. all elements that are in this set but not the others.)
    valuedlute
    valuedlute
    Oct 8, 2016
    18
    difflib
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1417 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 22:01 · PVG 06:01 · LAX 14:01 · JFK 17:01
    ♥ Do have faith in what you're doing.