python - 比较两个网络边缘列表
问题描述
我有两个列表 - master.txt 和它的一个子集 child.txt。我想在 master.txt 中打印 child.txt 中不存在的边缘
大师.txt
A B
B C
D F
子.txt
B A
C B
E F
输出:DF
我写了一个示例代码
file1 = open("master.txt", "r")
file2 = open("child.txt", "r")
probe_id = file1.readlines()
loc_names = file2.readlines()`
#flag=0
for i in probe_id:
i=i.rstrip()
probe_info=i.split("\t")
probe_info[0]=probe_info[0].strip()
probe_info[1]=probe_info[1].strip()
flag=0
for j in loc_names:
j=j.strip()
loc_names=j.split("\t")
loc_names[0]=loc_names[0].strip()
loc_names[1]=loc_names[1].strip() #throwing index out of range error
if (probe_info[0]==loc_names[0] and probe_info[1]==loc_names[1]) or (probe_info[0]==loc_names[1] and probe_info[1]==loc_names[0]):
flag=1
if flag==0:
print i
截至目前,当我拆分较小的文件时,我的索引超出了范围。请帮忙。此外,如果有任何其他更快的技术来做同样的事情,请告诉我。谢谢
解决方案
如果我正确理解您的要求,那么您只需要:
$ awk '
{ edge=($1>$2 ? $1 FS $2 : $2 FS $1) }
NR==FNR{ file1[edge]; next }
!(edge in file1)
' child.txt master.txt
D F
如果您想在子节点中找到不在 master 中的边,您只需翻转输入文件的顺序:
$ awk '
{ edge=($1>$2 ? $1 FS $2 : $2 FS $1) }
NR==FNR{ file1[edge]; next }
!(edge in file1)
' master.txt child.txt
E F
以上将非常快,因为它只是进行哈希查找。
推荐阅读
- python - 是否有一种 SQL 语法可以通过基于同一张表中第三列的相等值在 2 列中搜索来创建新列?
- sql - 在插入语句中使用 OUTPUT 时,如果您在 select 中指定 order by,它是否遵守 select 中指定的顺序?
- javascript - 如何从包含多个 URL 的字符串中正确隔离和存储 Google 文件 ID?
- javascript - 图像 onLoad 处理程序从未在 Jest 中调用
- ab-initio - 从头算图到 Java 的转换
- html - 我没有得到下拉菜单
- vb.net - Is there a way to change the text displayed in a DatePicker? Instead of "Select a date", show something else?
- ios - 如何解决 No + [RCTConvert AIR MapCoordinate Array] 错误:找到函数。在 iOS 上反应原生地图
- azure-active-directory - AzureAd 组和用户的 Webhook 订阅不起作用
- marko - markojs 中的可重复属性标签