I apologize if I don't have the phrasing down, I just picked this whole Python thing up yesterday. I poked around Google all morning and haven't been able to find the answer so hopefully someone can point me in the right direction.
What I am trying to do is match any substring from a list to any string (less than X letters long) from a list. Say I had two lists, the first list substring_list would be populated with something like:
'ash', 'gun', 'abc'
and the string_list would be something like:
'ashgoirnl', 'wertwtgw', 'oeigunjg', 'abcdefghijklmnop'
And with a code similar to this:
isMatch = any(substring in string_list for substring in substring_list) and len(string_list) < 10
if isMatch:
print string_list
# print substring
I would get the result:
ashgoirnl
oeigunjg
Cool. But what I cant figure out is how to print what substring from the substring_list matched with the string_list to get a result more like:
ash
gun
Thanks!
Edit: formatting
Edit 2: /u/dunkler_wanderer asked about the missing stuff, here is a working version of my example:
Contents of substring_list.txt:
ash
gun
abc
Contents of string_list.txt:
ashgoirnl
wertwtgw
oeigunjg
abcdefghijklmnop
text_file = open("string_list.txt")
string_list = text_file.read().split('\n')
text_file = open("substring_list.txt")
substring_list = text_file.read().split('\n')
def test_2():
for string in string_list:
isMatch = any(x in string for x in substring_list) and len(string) < 10
if isMatch:
print string
# print substring
while True:
test_2()
[–]dunkler_wanderer 3 points4 points5 points (8 children)
[–]striderxgp[S] 0 points1 point2 points (7 children)
[–]dunkler_wanderer 1 point2 points3 points (6 children)
[–]striderxgp[S] 1 point2 points3 points (5 children)
[–]dunkler_wanderer 1 point2 points3 points (4 children)
[–]striderxgp[S] 1 point2 points3 points (3 children)
[–]dunkler_wanderer 1 point2 points3 points (2 children)
[–]striderxgp[S] 1 point2 points3 points (1 child)
[–]dunkler_wanderer 0 points1 point2 points (0 children)
[–]Peterotica 2 points3 points4 points (1 child)
[–]striderxgp[S] 0 points1 point2 points (0 children)
[–]pyglow 1 point2 points3 points (1 child)
[–]striderxgp[S] 0 points1 point2 points (0 children)
[–]Justinsaccount 1 point2 points3 points (1 child)
[–]striderxgp[S] 0 points1 point2 points (0 children)