简体   繁体   中英

Cascading FOR loop outputs is there a better python 3 way

Right now, I have a word that I'm search for in a larger string if the word is not found then I insert the word in the larger sting. The output of the first FOR loop is cascaded into a second FOR loop.

This work's ok, with one or two but if I had 100's or 1000's of searchs it would be a pain to do this each time is there a better python 3 way?

As you can see from the example I'm looking for Monday in the first string monday is there so it just skips the second string does not have Monday so it adds monday in. I would have a new list at that point. So I put the output of the first loop into the input of the second loop to update the input.

Example:

Input: - addstring_sign = ["Monday was a snowing day", "Wednesday no snow"]

string_test1 = [] string_test2 = [] dictlines1 = ("Monday")

for loopblock in (addstring_sign):

    if addstring_sign.count(dictlines1) < 1:
       string_test1.append(loopblock[:len(loopblock)] + dictlines1 +'\n'+loopblock[len(loopblock):])

    else:
       string_test2.append(loopblock)
    addstring_sign1 = string_test1 + string_test2

OutPut of addstring_sign1 = ["Monday was a snowing day", "Wednesday no snow Monday"]

string_test3 = [] string_test4 = [] dictlines2 = ("Tuesday")

for loopblock2 in (addstring_sign1):

    if addstring_sign.count(dictlines2) < 1:
       string_test3.append(loopblock2[:len(loopblock2)] + dictlines2 +'\n'+loopblock2[len(loopblock2):])

    else:
       string_test4.append(loopblock)
    addstring_sign2 = string_test3 + string_test4`

output of addstring_sign2 = ["Monday was a snowing day Tuesday", "Wednesday no snow Monday Tuesday"]

I am lost on how to make this more efficient.

If nothing else, I think the below approaches are much cleaner than what you're currently doing and possibly appreciably faster (I need some time to set up proper test cases, to be added shortly).

addstring_sign = ["Monday was a snowing day", "Wednesday no snow"]
dictlines = ['Monday', 'Tuesday']

string_test1 = []
for string in addstring_sign:
    missing_strings = ' '.join([item for item in dictlines if item not in string])
    string_test1.append('{} {}'.format(string, missing_strings))

# Or a list comprehension for the same thing, probably not much faster
string_test2 = ['{} {}'.format(string, 
                ' '.join([item for item in dictlines if item not in string])) 
                for string in addstring_sign]

The next thing to do to speed this up is to convert dictlines to a set for larger problems. This is done simply with:

dictlines = set(dictlines)

Which will give you O(1) lookup - this will become more and more significant as dictlines grows in size. I struggled to get your existing code into an approach I could test on a larger scale but was unable. However, you can see that even with this tiny example, and without a set (here it makes little difference) that my initial approach is faster than your existing one:

%timeit my_for_loop(addstring_sign, dictlines)
1.53 µs ± 4.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit your_loop(addstring_sign, dictlines)
2.13 µs ± 8.69 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

TLDR;

for word in strings:
    sentences = [line if word in line else line + ' ' + word for line in sentences]

So you are given a list of sentences , of which you need to check if each contains a string . When one of them does not, you want to append the string to the sentence.

Let's first look at the second line of code and consider the case where you only have one word to search and append. This list comprehension iterates through your list of sentences then:

  1. Checks if the string is in the sentence
  2. Appends the string if it isn't in the sentence

At the end of the iterations, it reconstructs the list and assigns it back to the original list.

The first line of code than simply repeat this process for every word you wish to search and append.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM