I am looking for help in writing a script (preferably python), to compare two lists. The task:
I have list of filenames in file a.txt: Example:
ABC
BCD
DEF
EDC
and a strings list in list b.txt:
A
B
C
D
E
F
G
what I want script to do is "compare" those two lists and output result to file result.txt, where I can have every string from list a next to all matches of list b.txt so, for example, result.txt would look like for the above lists:
ABC|A,B,C
BCD|B,C,D
DEF|D,E,F
EDC|E,D,C
I might add, both lists are quite big, list a has 60k rows, list b few k.
Would appreciate the help a lot! thanks
Build lists A_list
, B_list
from datafiles
Write the result file looping over the filenames ( a
) provided by first list ( A_list
)
matches
list of the elements ( b
) of the second list ( B_list
) that are in the a
elementwith open("a.txt", 'r') as f:
A_list = f.read().splitlines()
with open("b.txt", 'r') as f:
B_list = f.read().splitlines()
with open('result.txt', 'w') as f:
for a in A_list:
matches = [b for b in B_list if b in a]
if not matches: continue # optional
f.write(f"{a}|{','.join(matches)}\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.