级联FOR循环输出是否有更好的Python 3方式 [英] Cascading FOR loop outputs is there a better python 3 way

查看:101
本文介绍了级联FOR循环输出是否有更好的Python 3方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有一个要在较大字符串中搜索的单词,如果找不到该单词,则将其插入较大的字符串中.第一个FOR循环的输出级联成第二个FOR循环.

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.

这项工作还可以,只有一两个,但是如果我有100或1000个搜索,每次有更好的python 3方式都很难做到这一点?

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.

示例:

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

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

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

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]

加快速度的下一步是将dictlines转换为set以解决更大的问题.只需使用以下方法即可完成

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)

这将为您提供O(1)查找-随着dictlines大小的增加,这将变得越来越重要.我努力将现有代码转换为可以大规模测试的方法,但是无法做到.但是,您可以看到,即使有一个很小的示例,也没有set(这里没有什么区别),我的初始方法比您现有的方法要快:

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)

这篇关于级联FOR循环输出是否有更好的Python 3方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆