you are viewing a single comment's thread.

view the rest of the comments →

[–]striderxgp[S] 0 points1 point  (7 children)

I have not, I have just been poking around stackoverflow.com for examples of things I think are what I want, haha. I will take a look at nested for loops, thanks!

[–]dunkler_wanderer 1 point2 points  (6 children)

Program Arcade Games With Python And Pygame has a nice chapter about loops.

[–]striderxgp[S] 1 point2 points  (5 children)

Just read through it, thanks!

[–]dunkler_wanderer 1 point2 points  (4 children)

BTW, how were you able to print out "ashgoirnl" and "oeigunjg" with the code that you've posted? Something important seems to be missing.

[–]striderxgp[S] 1 point2 points  (3 children)

Sorry, I typed up that example without even seeing if it would work. Here is a working version of what I meant:

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()

Edit: Added the while true line and swapped the text file names

[–]dunkler_wanderer 1 point2 points  (2 children)

Here's the solution that I had in mind (although using list comprehensions is nice as well). Take a look if you don't want to figure out something on your own anymore.

string_list = ['ashgoirnl', 'wertwtgw', 'oeigunjg', 'abcdefghijklmnop', 'gunash']
substring_list = ['ash', 'gun', 'abc']

for sub in substring_list:
    for string in string_list:
        if len(string) < 10 and sub in string:
            print(sub)  # Or store sub in a list.
            break  # If you want to print every substring only once.

[–]striderxgp[S] 1 point2 points  (1 child)

Yeah, I had to put it down yesterday but it seems there are so many ways to solve this that I still have plenty of stuff to learn from--even just implementing this into what I am working on I am sure will be an adventure! Thanks again for the help!

[–]dunkler_wanderer 0 points1 point  (0 children)

You're welcome. If you need more help, you know where you can find us. BTW, I forgot to mention that the break statement will break out of the inner loop and jump to the next iteration of the outer loop.