forked from sc-zhang/bioscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_two_lists.py
executable file
·30 lines (25 loc) · 804 Bytes
/
merge_two_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
import sys
def merge_two_lists(list1, col1, list2, col2, merged_list):
list_db = {}
with open(list2, 'r') as fin:
for line in fin:
data = line.strip().split()
list_db[data[col2]] = data
with open(list1, 'r') as fin:
with open(merged_list, 'w') as fout:
for line in fin:
data = line.strip().split()
key = data[col1]
if key in list_db:
data.extend(list_db[key][:col2])
data.extend(list_db[key][col2+1:])
fout.write("%s\n"%('\t'.join(data)))
if __name__ == "__main__":
if len(sys.argv) < 6:
print("Usage: python %s <list1> <col1> <list2> <col2> <merged_list>"%sys.argv[0])
else:
list1, col1, list2, col2, merged_list = sys.argv[1:]
col1 = int(col1)-1
col2 = int(col2)-1
merge_two_lists(list1, col1, list2, col2, merged_list)