求教大神
文本 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
求教大神
文本 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
|
1
想做的是如果文本 A 有 ip 存在于文本 B 就将它输出
|
|
zhidianOct 7, 2016
2
选你能用的编程语言, 读入文本 A, 扫描出所有的 IP, 存入 dict 中, 读入文本 B,
依次扫描, 遇到 ip, 如果 ip 在 dict 中, 则输出之. that's easy. |
|
zhuangzhuang1988Oct 7, 2016
3
|
|
northislandOct 7, 2016
4
N ( A )数量个 String Searching 问题(Substring)
(我记得最佳解法是用 KMP 算法) 欢迎拍砖~ |
|
northislandOct 7, 2016
5
后悔了,这是规则文本。
提取 IP ,搜索 IP 查找表,就可以了。 |
|
ldbC5uTBj11yaeh5Oct 7, 2016
6
comm -12 a.txt <(awk '{print $2}' b.txt)
|
|
ldbC5uTBj11yaeh5Oct 7, 2016
7
靠,看错了,居然是 python 节点。
(要啥自行车,用 shell 比 python 要快哒。) |
|
8
@jigloo 大哥 不好意思 我需要 Python 哈哈
|
|
9
@jigloo 因为这段代码要 放进项目用
|
|
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 |
|
northislandOct 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' |
|
12
@northisland 发送感谢
|
|
13
@jigloo 感谢已发送
|
|
14
@northisland KMP 是单个匹配,多个匹配可以试试 AC 自动机。
不过规则文本的话更简单的优化方式茫茫多 |
|
orange88Oct 7, 2016 via Android
15
用 pandas 可以搞定
|
|
lululauOct 7, 2016
16
grep -vFf <(cut -d ' ' -f 2 B) A
|
|
SlipStupigOct 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.) |
|
valuedluteOct 8, 2016
18
difflib
|