gpt4 book ai didi

python - 比较两个网络边缘列表

转载 作者:行者123 更新时间:2023-11-28 22:17:04 25 4
gpt4 key购买 nike

我有两个列表 - 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

如果您想在 child 中找到不在 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 - 比较两个网络边缘列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51665542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com