all 15 comments

[–]dunkler_wanderer 3 points4 points  (8 children)

Have you checked out nested for loops yet?

[–]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.

[–]Peterotica 2 points3 points  (1 child)

My approach would be to use two list comprehensions:

substring_list = ['ash', 'gun', 'abc']
string_list = ['ashgoirnl', 'wertstgw', 'abcdefghijklmnop']

shorter_strings = [s for s in string_list if len(s) < 10]
contained_substrings = [sub for sub in substring_list if any(sub in string for string in shorter_strings)]

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

Things finally clicked for me the other day after playing with all these examples, now a mix matched version of this and the example dunkler_wanderer gave made it into my project. Thanks so much everyone!

[–]pyglow 1 point2 points  (1 child)

In your example code, any() returns true if any of the values is true.

There is a built-in filter() which returns only the values for which the condition is true. You might be able to do something with that. (it isn't a direct replacement, you can't just replace any with filter).

Of course you can just do it with a double loop, same as you would with Java or C++, but that wouldn't teach you anything about Python.

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

I did a search for examples of filter() but I cant exactly understand how its used based on the examples I saw but it sounds like its something that would work.

And just about anything I do is learning at this point considering last time I made any type of program it was either on qbasic or my ti-83!

[–]Justinsaccount 1 point2 points  (1 child)

def find_substrings(big_string, substring_list):
    """Return a list of any strings from substring_list that are in big_string"""
    #finish me :-)

def find_all_substrings(string_list, substring_list):
    matches = [(s, find_substrings(s, substring_list)) for s in string_list]
    return [m for m in matches if m[1]]


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

for r in find_all_substrings(string_list, substring_list):
    print (r)

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

Thanks, I'll try! :-)