-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial_match1.py
More file actions
120 lines (94 loc) · 3.25 KB
/
partial_match1.py
File metadata and controls
120 lines (94 loc) · 3.25 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Get pairs of partial matching strings from one list compared with another. """
__author__ = "Ray Stefancsik"
__version__ = "1.0"
#########################################################################
# Modules to import
#### Import command-line parsing module from the Python standard library.
#########################################################################
import argparse
import sys
### Mandatory positional arguments
parser = argparse.ArgumentParser( description='1. Take two sets of elements as lists. 2. Go through the elements in the first list and check if they partially match any elements in the second list. 3. Print the results to standard output.' )
parser.add_argument( 'your_list1', help='Path to your first list. FORMAT: Use a plain text file with one list element per line. Lines that start with # are ignored.' )
parser.add_argument( 'your_list2', help='Path to your second list file. FORMAT: Use a plain text file with one list element per line. Lines that start with # are ignored.' )
### Optional, but mutually exclusive user options:
group = parser.add_mutually_exclusive_group()
group.add_argument('-1', '--matching', help='Print the matching pairs only.', action='store_true')
group.add_argument('-2', '--no_match', help='Print the non-matching pairs only.', action='store_true')
######################################################################
# parse input data files
args = parser.parse_args()
# data input filenames
fname = args.your_list1
# Create an empty list for set 1
list1=list()
###############################
# parse the 1st input data file
###############################
# Create an empty list for set 1
list1=list()
# Check if the file exists before opening it.
fname = args.your_list1
try:
fhand = open( fname )
except:
print('File cannot be opened:', fname)
exit()
# parse the data file
for line in fhand:
if line.startswith('#'): # skip comments and header lines
continue
line = line.rstrip()
list1.append(line)
# close file
fhand.close()
###############################
# parse the 2nd input data file
###############################
# Create an empty list for set 1
list2=list()
# Check if the file exists before opening it.
fname = args.your_list2
try:
fhand = open( fname )
except:
print('File cannot be opened:', fname)
exit()
# parse the data file
for line in fhand:
if line.startswith('#'): # skip header lines
continue
line = line.rstrip()
list2.append(line)
# close file
fhand.close()
# initialise two empty lists to store the results
matching = []
no_match = []
for e2 in list2:
for e1 in list1:
if e1 in e2:
matching.append((e1, e2))
else:
no_match.append((e1, e2))
# print the list of partially matching pairs
if args.matching:
print("#### matching:")
for i in matching:
print(i)
sys.exit() # stop here
# print the list of non-matching pairs
if args.matching:
print("#### non-matching:")
for i in no_match:
print(i)
sys.exit() # stop here
# print everything unless told otherwise if you get so far as this point
print("#### matching:")
for i in matching:
print(i)
print("#### non-matching:")
for i in no_match:
print(i)