python - 边列表中节点的唯一列表
问题描述
我有一个大边缘列表(约 2600 万),前两列作为节点,可选列的数量可变:
Node1 Node2 OptionalCol1 OptionalCol2 ...
Gene A Gene D -- --
Gene C Gene F -- --
Gene D Gene C -- --
Gene F Gene A -- --
我想要一个文本文件,其中包含结合两列的非冗余节点列表。输出:
Gene A
Gene D
Gene C
Gene F
我的python代码:
file1 = open("input.txt", "r")
node_id = file1.readlines()
node_list=[]
for i in node_id:
node_info=i.split()
node_info[0]=node_info[0].strip()
node_info[1]=node_info[1].strip()
if node_info[0] not in node_list:
node_list.append(node_info[0])
if node_info[1] not in node_list:
node_list.append(node_info[1])
print node_list
可以用 awk 做到这一点吗?谢谢
解决方案
假设分隔符是制表符 ( \t
)。如果它是一堆空间(一堆不止一个)而不是-F"\t"
使用-F" +"
::
$ awk -F"\t" 'NR>2{a[$1];a[$2]}END{for(i in a)print i}' file
Gene A
Gene C
Gene D
Gene F
输出没有任何特定的顺序,但它可能是。解释:
$ awk -F"\t" '
NR>2 { # starting on the third record
a[$1] # hash first...
a[$2] # and second columns
}
END { # after all that hashing
for(i in a) # iterate whole hash
print i # and output
}' file
推荐阅读
- matlab - 使用 matlab 添加 powerpoint 幻灯片:错误使用 mlreportgen.ppt.Presentation/add 找不到布局:“空白”
- excel - 我试图在用户表单中有一个列表框,当我选择一个特定的列表项时,我希望一个文本框可见
- r - 将相关方程写入 R
- amazon-web-services - 什么是 $path 伪列?它在 Athena (Presto) 中有什么用?
- android - 如何使用 selection 和 selectionArgs 通过 android ContentResolver.query 在 sdcard 中搜索特定文件名?
- delphi - 为什么编译器在 TTabSheet 中先执行 OnShow 事件,然后再执行 OnHide?
- java - Spring Boot 中的单元测试或集成测试
- python - 尝试在 @event 中访问时出现 JSON 编辑问题,我该如何解决?
- algorithm - 如果这些问题是 NP-Complete 的,那么如何有多项式时间算法来解决它们?
- web-services - Docker-Compose API 调用和端点容器化